Professional Documents
Culture Documents
Matlab 4
Matlab 4
Problem 1 (5 Points): Calculate and plot the derivative of the following function over the range 𝑥 =
𝜋/10 to 9𝜋/10 using the Backward-Difference Method described in class (shown below). You must use a FOR
LOOP to solve this problem. Also, plot the original function on the graph.
𝑦 = cot(𝑥)
9𝜋/10
𝑦 = 𝑦0 + ∫ −[csc(𝑥)]2 dx
𝜋/10
from 𝑥 = 𝜋/10 to 9𝜋/10 using the Trapezoidal Integration Method described in class (shown below). You
must use a FOR LOOP to solve this problem. Use the following value: 𝑦0 = 3.0777. Use 101 points from 𝑥 =
𝜋/10 to 9𝜋/10. Also plot the function being integrated −[csc(𝑥)]2 on the graph.
Problem 3 (5 Points): Create a MATLAB script file that uses the Euler Method discussed in class (shown
below) to solve the following differential equation. You must use a FOR LOOP to solve this problem. Plot the
solution from 0 ≤ 𝑡 ≤ 10 seconds.
Euler Method:
𝑑𝑦 𝑦𝑘+1 − 𝑦𝑘
≅
𝑑𝑡 ∆𝑡
Problem 4 (5 Points): Using SIMULINK, solve the following differential equation, and plot 𝑦 from 0 ≤ 𝑡 ≤
10 seconds.
N = 1000
x = linspace(pi/10,9*pi/10,N);
y = cot(x);
% dydxexact = -(csc(x)).^2;
for k = 1:N-1
dydx(k) = (y(k+1) - y(k))/(x(k+1) - x(k));
end
% plot(x,y,'k-',x(1:N-1),dydx(1:N-1),'k:',x,dydxexact,'k-.')
plot(x,y,'k-',x(1:N-1),dydx(1:N-1),'k-.')
xlabel('x'), ylabel('y'), title('Problem 1, ME 1020: Scott Thomas')
% legend('y = cot(x)','dy/dx Numerical','dy/dx Exact','Location','Best')
legend('y = cot(x)','dy/dx','Location','Best')
N =
1000
N = 101
x = linspace(pi/10,9*pi/10,N);
intyexact = cot(x);
y = -(csc(x)).^2;
inty(1) = 3.0777;
for k = 1:N-1
inty(k+1) = inty(k) + 0.5*(x(k+1) - x(k))*(y(k)+y(k+1));
end
% plot(x,y,'k-',x,inty,'k:',x,intyexact,'k-.')
plot(x,y,'k-',x,inty,'k:')
xlabel('x'), ylabel('y'), title('Problem 2, ME 1020: Scott Thomas')
% legend('-(csc(x))^2','y (Numerical)','y (Exact)','Location','Best')
legend('-(csc(x))^2','Integral','Location','Best')
N =
101
N = 10000;
dt = 0.001
y(1) = 8.0;
t(1)=0;
for k = 1:N
y(k+1) = y(k) + dt*(-0.8*(y(k))^(2) + 4.4*(exp(-0.008*t(k)))*(sin(8*t(k))));
t(k+1) = t(k) + dt;
end
plot(t,y)
xlabel('t'), ylabel('y'), title('Problem 3, ME 1020: Scott Thomas')
dt =
1.0000e-03
5 4 3 2 1 0
3 9 2 1 2 0
Problem 4:
5 4 3 2 1 0
6 4 3 0 5 0