MAT275 Matlab4

  • Upload
    nav4evr

  • View
    147

  • Download
    4

Embed Size (px)

DESCRIPTION

lab 4

Citation preview

MAT 275 MATLAB Assignment #4Question 1Part afunction LAB04ex1clct0 = 0; tf = 40; y0 = [-1;0];%sets time initial and final and initial conditions[t,Y] = ode45(@f,[t0,tf],y0,[]);[t,Y]u1 = Y(:,1); u2 = Y(:,2); % y in output has 2 columns corresponding to u1 and u2figure(1);%sets first figureplot(t,u1,'b.-',t,u2,'r.-');%plots figuresgrid onylim([-1.5,1.5]);legend('y(t)','v(t)=y''(t)');%puts up legend for first plotfigure(2)plot(u1,u2,'black.-'); axis square; xlabel('y'); ylabel('v=y''');axis([-1,1,-1.5,1.5]);%sets the axis limitsset(gca,'XTick',-1:.2:1)%sets the ticks on the plot on the x axisgrid on%puts the grid onend%----------------------------------------------------------------------function dYdt = f(t,Y)%defines the ODEy=Y(1); v=Y(2);%sets the vectors for each y and vdYdt = [v; cos(t)-4*v-3*y];%the IVP equationend

%Part bY is a maximum when t=2.1340, 7.3562, 13.6632, 19.9324, 26.2151, 32.4983, 38.7814

2.0365 -0.0931 0.0448 2.1340 -0.0903 0.0125 2.2316 -0.0905 -0.0162 7.2771 0.2210 0.0265 7.3562 0.2224 0.0087 7.4353 0.2224 -0.0091 7.5057 0.2212 -0.0248 13.5866 0.2228 0.0194 13.6632 0.2236 0.0023 13.7398 0.2231 -0.0148 19.8555 0.2225 0.0226 19.9324 0.2235 0.0054 20.0093 0.2233 -0.0118 26.1381 0.2225 0.0227 26.2151 0.2235 0.0056 26.2920 0.2233 -0.0117 32.4213 0.2225 0.0227 32.4983 0.2235 0.0056 32.5752 0.2233 -0.0117 38.7045 0.2225 0.0227 38.7814 0.2235 0.0056 38.8584 0.2233 -0.0117

Part c

The long term behavior of y seems sinusoidal fluctuating from -.2 to .2235

Part d

Based on the graphs, the equation stabilizes at the same frequency and amplitude as it does with the initial conditions of y(0)=0,v(0)=-1. Based on the picture of y vs y, we can see that y starts positively while y starts negatively compared with the old initial conditions.

Question 2Part afunction LAB04ex2clct0 = 0; tf = 40; y0 = [-1;0];[t,Y] = ode45(@f,[t0,tf],y0,[]);[t,Y]u1 = Y(:,1); u2 = Y(:,2); % y in output has 2 columns corresponding to u1 and u2figure(1);plot(t,u1,'b.-',t,u2,'r.-');grid onylim([-1.5,1.5]);legend('y(t)','v(t)=y''(t)');figure(2)plot(u1,u2,'black.-'); axis square; xlabel('y'); ylabel('v=y''');axis([-1,1,-1.5,1.5]);set(gca,'XTick',-1:.2:1)grid onend%----------------------------------------------------------------------function dYdt = f(t,Y)y=Y(1); v=Y(2);dYdt = [v; cos(t)-4*v*y^2-3*y];end

Part bIn the short term, the solution of figs L4g become stable much more quickly, reaching the cyclic solution. On the other hand, the solution of Figs L4h reach stability much more slowly. The behavior of the equation is much more erratic and takes longer to reach steady state.

Part cThe long term behavior shows that the solution of L4.4 has an amplitude of .47 while the solution of L4.5 has an amplitude of double that, .95 or so. Both, however, have the same period for their oscillations.

Part dfunction LAB04ex2dclcclear allt0 = 0; tf = 40; y0 = [-1;0];[t,Y] = ode45(@f,[t0,tf],y0,[]);[t,Y]u1 = Y(:,1); u2 = Y(:,2); % y in output has 2 columns corresponding to u1 and u2[te,Ye]=euler(@f,[0,40],y0,400);%calls eulers method using the function f, initial conditions and number of stepsu3=Ye(:,1); u4=Ye(:,2);figure(1);plot(t,u1,'black.-',te,u3,'r.-');grid onylim([-1.5,1.5]);legend('y(t)','euler');grid onend%----------------------------------------------------------------------function dYdt = f(t,Y)y=Y(1); v=Y(2);dYdt = [v; cos(t)-4*v*y^2-3*y];end

The solutions are nearly identical. The ode45 solution is a little more accurate and approaches the cyclic behavior a little faster than the euler solution.

Question 3function LAB04ex3clcclear allt0 = 0; tf = 40; y0 = [-1;0];[t,Y] = ode45(@f,[t0,tf],y0,[]);u1 = Y(:,1); u2 = Y(:,2); % y in output has 2 columns corresponding to u1 and u2[te,Ye]=euler(@f,[0,40],y0,400);u3=Ye(:,1); u4=Ye(:,2);figure(1);plot(t,u1,'black.-',te,u3,'r.-');grid onylim([-1.5,1.5]);legend('y(t)','euler');grid onend%----------------------------------------------------------------------function dYdt = f(t,Y)y=Y(1); v=Y(2);dYdt = [v; cos(t)-4*v*y-3*y];end

When solving numerically for the solution of the IVP, the solution looks somewhat different to that of L4.7. For one, the solution looks as if it deviates much more at the beginning than the solution of L4.7. When trying to run the numerically solving of this IVP, MATLAB gave the error message of Warning: Failure at t=3.774765e+000. Unable to meet integration tolerances without reducing the step size below the smallest value allowed (7.105427e-015) at time t. As a result, beyond the when t=3.77, the numerical solution of this ode could not be solved. Initial behavior is known, but final behavior is not because of this.

Question 4Part afunction LAB04ex4clct0 = 0; tf = 40; y0 = [-1;0;4];[t,Y] = ode45(@f,[t0,tf],y0,[]);u1 = Y(:,1); u2 = Y(:,2); u3=Y(:,3); % y in output has 2 columns corresponding to u1 and u2figure(1);plot(t,u1,'b.-',t,u2,'r.-',t,u3,'.-k');grid onylim([-1.5,1.5]);legend('y(t)','v(t)=y''(t)','w=y''''');figure(2)plot3(u1,u2,u3,'k.-'); axis square; xlabel('y'); ylabel('v=y'''); zlabel('w=y''''');axis([-1,1,-1.5,2,-4,4]);grid onview(-40,60);end%----------------------------------------------------------------------function dYdt = f(t,Y)y=Y(1); v=Y(2); w=Y(3);dYdt = [v;w; -sin(t)-4*y^2*w-8*y*v^2-3*v];end

Part bAt the beginning, it looks as if L4i is more erratic than L4h, but both quickly go to the same oscillation in roughly the same time. The amplitude of the solution of each of them is nearly identical as is the period. In addition, looking at the phase plots of each, it seems as if they are nearly identical with the one from Fig L4i

Part c

When differentiated, the ODE of L4.7 is the same as the ODE in L4.8. They are identical equations.

Part dThe equation in L4.7 satisfies the initial condition of L4.8 because when you set t=0 . When we take the initial conditions of L4.8 , , which is exactly the same as the initial conditions of L4.8.