Matlab 3
Matlab 3
Problem 1 (5 Points): Create a MATLAB script file to calculate and plot the function
3
2 2
𝑦= ∫ 𝑒 −𝑥 dx
√𝜋 0
from 𝑥 = 0 to 3 using the Trapezoidal Integration Method described in class (shown below). You must use a
FOR LOOP to solve this problem. Use 101 points from 𝑥 = 0 to 3.
Problem 2 (5 Points): Create a MATLAB script file that uses the Euler Method to solve the following
differential equation for 𝑦(𝑡). Plot the solution from 0 ≤ 𝑡 ≤ 10 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.
2 3 1
𝑦̈ − 𝜇 (1 − 𝑦 2 )𝑦̇ + 𝑦 = sin ( 𝑡) , 𝜇 = 2.0, 𝑦(𝑡 = 0) = 1.0, 𝑦̇ (𝑡 = 0) =
3 2 2
Problem 3 (5 Points): Use Simulink to solve the differential equation given in Problem 2 for 𝑦(𝑡).
Problem 4 (5 Points): The equations for a saw-tooth wave are given below:
2 𝜋 𝜋 𝜋
𝑉 =1− (𝑡 + ) for − ≤𝑡<
𝜋 2 2 2
2 𝜋 𝜋 3𝜋
𝑉 = −1 + (𝑡 − ) for ≤𝑡<
𝜋 2 2 2
2 3𝜋 3𝜋 5𝜋
𝑉 = 1− (𝑡 − ) for ≤𝑡<
𝜋 2 2 2
2 5𝜋 5𝜋 7𝜋
𝑉 = −1 + (𝑡 − ) for ≤𝑡<
𝜋 2 2 2
where 𝑉 is the voltage generated and time 𝑡 is in seconds. Create a MATLAB Script File using a WHILE
LOOP to plot the voltage 𝑉 versus time 𝑡. Provide a plot title and labels for the axes. Suppress all unnecessary
output.
Problem 1: Final Exam, Fall 2017
clc
clear
close all
N = 101
x = linspace(0,3,N);
y = exp(-x.^2);
int(1) = 0;
for k = 1:N-1
int(k+1) = int(k) + 0.5*(x(k+1) - x(k))*(y(k)+y(k+1));
end
erfx = 2/sqrt(pi)*int;
plot(x,erfx)
grid on
xlabel('x'), ylabel('erf(x)'), title('Problem 1, ME 1020: Scott Thomas')
N =
101
mu = 2.0;
N = 100;
deltat = 0.1;
t(1) = 0;
x1(1) = 1.0;
x2(1) = 1/2;
for k = 1:N
x1(k+1) = x1(k) + deltat*x2(k);
x2(k+1) = x2(k) + deltat*(mu*(1-x1(k)^2)*x2(k) - x1(k) + 2/3*sin(3/2*t(k)));
t(k+1) = t(k) + deltat;
end
NN = 1000;
ddeltat = 0.01;
tt(1) = 0;
xx1(1) = 1.0;
xx2(1) = 1/2;
for k = 1:NN
xx1(k+1) = xx1(k) + ddeltat*xx2(k);
xx2(k+1) = xx2(k) + ddeltat*(mu*(1-xx1(k)^2)*xx2(k) - xx1(k) + 2/3*sin(3/2*tt(k)));
tt(k+1) = tt(k) + ddeltat;
end
NNN = 10000;
dddeltat = 0.001;
ttt(1) = 0;
xxx1(1) = 1.0;
xxx2(1) = 1/2;
for k = 1:NNN
xxx1(k+1) = xxx1(k) + dddeltat*xxx2(k);
xxx2(k+1) = xxx2(k) + dddeltat*(mu*(1-xxx1(k)^2)*xxx2(k) - xxx1(k) + 2/3*sin(3/2*ttt(k)));
ttt(k+1) = ttt(k) + dddeltat;
end
NNNN = 100000;
ddddeltat = 0.0001;
tttt(1) = 0;
xxxx1(1) = 1.0;
xxxx2(1) = 1/2;
for k = 1:NNNN
xxxx1(k+1) = xxxx1(k) + ddddeltat*xxxx2(k);
xxxx2(k+1) = xxxx2(k) + ddddeltat*(mu*(1-xxxx1(k)^2)*xxxx2(k) - xxxx1(k) + 2/3*sin(3/2*tttt(k)));
tttt(k+1) = tttt(k) + ddddeltat;
end
plot(t,x1,tt,xx1,ttt,xxx1,tttt,xxxx1),xlabel('t'),ylabel('y')
title('Problem 2: Final Exam, Fall 2017, ME 1020')
legend('\Delta t = 0.1', '\Delta t = 0.01', '\Delta t = 0.001','\Delta t = 0.0001', 'Location','Best')
text(0.3, 2.25,'Grid Independent for Delta t <= 0.001')
Published with MATLAB® R2017a
Problem 3:
open_system('problem3')
sim('problem3')
plot(y(:,1),y(:,2))
Published with MATLAB® R2017a
Problem 4: Final Exam, Fall 2017
clc
clear all
close all
t = -pi/2;
dt = 0.05;
k = 1;
while t <= 7*pi/2
if t >= -pi/2 && t < pi/2
y = 1 - 2/pi*(t + pi/2);
elseif t >= pi/2 && t < 3*pi/2
y = -1 + 2/pi*(t - pi/2);
elseif t >= 3*pi/2 && t < 5*pi/2
y = 1 - 2/pi*(t - 3*pi/2);
else
y = -1 + 2/pi*(t - 5*pi/2);
end
tplot(k) = t;
yplot(k) = y;
k = k + 1;
t = t + dt;
end
plot(tplot,yplot), xlabel('Time t (sec)'),ylabel('Voltage V')
title('Problem 4: Final Exam, Fall 2017')