Matlab 5
Matlab 5
Problem 1 (4 Points): Create a MATLAB script file to calculate and plot the derivative of the following
function from 0 ≤ 𝑥 ≤ 𝜋 using the Central Difference Method described in class (shown below). You must use
a for loop with a sufficient number of terms to solve this problem. Provide a plot title and labels for the axes.
𝑦 = 3cos(2𝑥)
Problem 2 (8 Points): Create a MATLAB script file that uses the Euler Method to solve the following
differential equation for 𝑦(𝑡). Plot the solution from 0 ≤ 𝑡 ≤ 2 seconds. Provide a plot title and labels for the
axes. You must use a for loop to solve this problem. Show that your solution for 𝑦(𝑡) is independent of the
time step size, as discussed in class.
Problem 3 (8 Points): The following differential equation describes the motion of a mass connected to a spring
and a viscous damper. Using SIMULINK, plot the solution from 0 ≤ 𝑡 ≤ 2 seconds.
N = 100;
x = linspace(0,pi,N);
y = 3*cos(2*x);
for k = 2:N-1
dydx(k) = (y(k+1) - y(k-1))/(x(k+1) - x(k-1));
xplot(k) = x(k);
end
plot(xplot,dydx),xlabel('x'),ylabel('dy/dx')
title('Problem 1: Final Exam, Fall 2016, ME 1020')
4 3 2 1 0
11 5 11 1 0
Problem 2:
% Problem 2
clc, clear all, close all
N1 = 20;
deltat1 = 0.1;
t1(1) = 0;
x1(1) = 0;
x2(1) = 10;
for k = 1:N1
x1(k+1) = x1(k) + deltat1*x2(k);
x2(k+1) = x2(k) + deltat1*(-25*x1(k) + 10/3*sin(2*t1(k)));
t1(k+1) = t1(k) + deltat1;
end
N2 = 200;
deltat2 = 0.01;
t2(1) = 0;
xx1(1) = 0;
xx2(1) = 10;
for k = 1:N2
xx1(k+1) = xx1(k) + deltat2*xx2(k);
xx2(k+1) = xx2(k) + deltat2*(-25*xx1(k) + 10/3*sin(2*t2(k)));
t2(k+1) = t2(k) + deltat2;
end
N3 = 2000;
deltat3 = 0.001;
t3(1) = 0;
xxx1(1) = 0;
xxx2(1) = 10;
for k = 1:N3
xxx1(k+1) = xxx1(k) + deltat3*xxx2(k);
xxx2(k+1) = xxx2(k) + deltat3*(-25*xxx1(k) + 10/3*sin(2*t3(k)));
t3(k+1) = t3(k) + deltat3;
end
N4 = 20000;
deltat4 = 0.0001;
t4(1) = 0;
xxxx1(1) = 0;
xxxx2(1) = 10;
for k = 1:N4
xxxx1(k+1) = xxxx1(k) + deltat4*xxxx2(k);
xxxx2(k+1) = xxxx2(k) + deltat4*(-25*xxxx1(k) + 10/3*sin(2*t4(k)));
t4(k+1) = t4(k) + deltat4;
end
plot(t1,x1,t2,xx1,t3,xxx1,t4,xxxx1),xlabel('t'),ylabel('y')
title('Problem 2: Final Exam, Fall 2016, ME 1020')
legend('\Delta t = 0.1', '\Delta t = 0.01', '\Delta t = 0.001', '\Delta t = 0.0001','Location','Best')
text(0.1, 13,'Grid Independent for Delta t <= 0.001')
Published with MATLAB® R2012b
8 7 6 5 4 3 2 1 0
4 3 5 6 4 6 0 0 0
Problem 3:
8 7 6 5 4 3 2 1 0
10 10 3 1 1 3 0 0 0
Euler Solution:
N1 = 20000;
deltat1 = 0.0001;
t1(1) = 0;
x1(1) = 0;
x2(1) = 10;
for k = 1:N1
x1(k+1) = x1(k) + deltat1*x2(k);
x2(k+1) = x2(k) + deltat1*(-5/3*x2(k) - 25*x1(k) + 10/3*sin(2*t1(k)));
t1(k+1) = t1(k) + deltat1;
end
plot(t1,x1),xlabel('t'),ylabel('y')
title('Problem 3: Final Exam, Fall 2016, ME 1020')