In: Advanced Math
Question 5.
Use MATLAB to solve for and plot the response of the following models for 0≤t ≤1.5, where the input is f (t) =5t and the initial conditions are zero
a. 3¨ x +21˙ x +30x = f (t) b. 5¨
A (Turn in the MATLAB script and answers from MATLAB, .m file, screen shots if needed)
B (Turn in the MATLAB plot with t being time in SI units)
C Comment on the response the analytical solution compared with the MATLAB and the plots. (Do the calculations and MATLAB agree ? Why and Why not.? Do the plots make sense?)
%Matlab code for solving differential equation
clear all
close all
syms x(t)
eqn = 3.*diff(x,t,2)+21.*diff(x,t,1)+30.*x(t)== 5.*t;
Dx = diff(x,t);
cond = [x(0)==0, Dx(0)==0];
ySol(t) = dsolve(eqn,cond);
fprintf('Displaying the analytical solution y(t)=\n')
disp(ySol(t))
%Initial conditions for ode
x0=[0;0];
%Solution for equation using ode45
%minimum and maximum
time span
tspan=[0 1.5];
%Solution of ODEs using
ode45 matlab function
sol= ode45(@(t,x)
odefcn1(t,x), tspan, x0);
%Equally splitting time
into .02 sec interval for 0 to 50
t1 =
linspace(tspan(1),tspan(end),1001);
%yy is the corresponding
x y v and z
yy1 =
deval(sol,t1);
%Plotting of x(t) y(t) and z(t)
figure(1)
hold on
plot(t1,ySol(t1))
plot(t1,yy1(1,:))
legend('Exact solution','Numerical solution')
title('Plot for x vs. t')
xlabel('time')
ylabel('X(t)')
box on; grid on;
%Function for evaluating the ODE
function dxdt = odefcn1(t,x)
eq1=x(2);
eq2=(5*t-21.*x(2)-30.*x(1))./3;
%Evaluate the ODE for our present problem
dxdt = [eq1;eq2];
end
%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%