1 / 8100%
EXERSIZE 1
A)
function LAB04ex1
t0 = 0;
tf = 40; %Defines timespan
y0 = [-0.5;0.5]; %Sets initial conditions
[t,Y] = ode45(@f,[t0,tf],y0); %Defines ODE function
y = Y(:,1);
v = Y(:,2); %Y in output has 2 columns corresponding to y and v
figure(1);
plot(t,y,'b-',t,v,'r-'); %Plots values for y(t) and v(t)
legend('y(t)','v(t)'); %Creates legend
xlabel('t');
ylabel('v(t)=y''(t)');
grid on;
hold on;
xlim([0,40]); %Sets x limits
ylim([-3.9,3.9]); %Sets y limits
figure(2)
plot(y,v);
axis square;
xlabel('y');
ylabel('v(t)=y''(t)');
ylim([-3.9,3.9]);
xlim([-3,3]);
grid on;
end
%----------------------------------------------------------------------
function dydt = f(t,Y)
y = Y(1); v = Y(2);
dydt = [ v ; 7*sin(t)-5*v-4*y ];
end
1
vit)
vit)
10
15,
20 25
30 35
40
2
B)
Last 3 values of t for which y is a local maximum.
21.3956
27.6956
33.9896
C)
The long-term behavior seems to be a continuous sinusoidal wave.
D)
The long-term behavior seems to have stayed the same. A continuous sinusoidal wave.
EXERSIZE 2
A)
function LAB04ex2
t0 = 0;
tf = 40; %Defines timespan
y0 = [-0.5;0.5]; %Sets initial conditions
[t,Y] = ode45(@f,[t0,tf],y0); %Defines ODE function
y = Y(:,1);
v = Y(:,2); %Y in output has 2 columns corresponding to y and v
figure(1);
plot(t,y,'b-',t,v,'r-'); %Plots values for y(t)
legend('y(t)','v(t)'); %Creates legend
xlabel('t');
ylabel('v(t)=y''(t)');
grid on;
hold on;
xlim([0,40]); %Sets x limits
ylim([-3.9,3.9]); %Sets y limits
figure(2)
plot(y,v);
axis square;
3
This study source was downloaded by 100000872770707 from CourseHero.com on 09-29-2023 08:38:11 GMT -05:00
xlabel('y');
ylabel('v(t)=y''(t)');
ylim([-3.9,3.9]);
xlim([-3,3]);
grid on;
[t,Y(:,1), Y(:,2)]
end
%----------------------------------------------------------------------
function dydt = f(t,Y)
y = Y(1); v = Y(2);
dydt = [ v ; 7*sin(t)-5*y^2*v-4*y ];
end
4
B)
The solution to the non-linear ODE seems to have a larger and inconsistent amplitude compare to the
linear ODE. This is short term behavior of the solution.
C)
In the first equation, it is linear, so the amplitude is constant. In the second equation, it is non-linear, and
its amplitude is constantly changing. It is also periodic.
D)
function LAB04ex2
t0 = 0;
tf = 40; %Defines timespan
y0 = [-0.5;0.5]; %Sets initial conditions
[t,Y] = ode45(@f,[t0,tf],y0); %Defines ODE function
[te,Ye] = euler(@f,[t0,tf],y0,400); %Defines the euler method
y = Y(:,1);
v = Y(:,2);
ye = Ye(:,1);
figure(1);
plot(t,y,'b-',te,ye,'r-','Linewidth',2);
grid on;
hold on;
legend('y(t)[ode45]','y(t)[Euler]');
xlim([0,40]); %Sets x limits
ylim([-3.9,3.9]); %Sets y limits
5
end
%----------------------------------------------------------------------
function dydt = f(t,Y)
y = Y(1); v = Y(2);
dydt = [ v ; 7*sin(t)-5*y.^2*v-4*y ];
end
The solutions are close, but not identical. If you increase the N value, they will get closer and closer.
Eventually they will be identical. I tested it at an N value of 1000, and they looked identical.
EXERSIZE 3
function LAB04ex3
t0 = 0;
tf = 40; %Defines timespan
y0 = [-0.5;0.5]; %Sets initial conditions
[t,Y] = ode45(@f,[t0,tf],y0); %Defines ODE function
y = Y(:,1);
v = Y(:,2); %Y in output has 2 columns corresponding to y and v
figure(1);
plot(t,y,'b-',t,v,'r-'); %Plots values for y(t)
legend('y(t)','v(t)'); %Creates legend
xlabel('t');
ylabel('v(t)=y''(t)');
grid on;
hold on;
6
xlim([0,40]); %Sets x limits
ylim([-3.9,3.9]); %Sets y limits
figure(2)
plot(y,v);
axis square;
xlabel('y');
ylabel('v(t)=y''(t)');
ylim([-3.9,3.9]);
xlim([-3,3]);
grid on;
[t,Y(:,1), Y(:,2)]
end
%----------------------------------------------------------------------
function dydt = f(t,Y)
y = Y(1); v = Y(2);
dydt = [ v ; 7*sin(t)-5*y*v-4*y ];
end
MATLAB gives the following error message:
Warning: Failure at t=4.612831e+00. Unable to meet integration tolerances without reducing the step
size below the smallest value allowed (1.421085e-14) at time t.
> In ode45 (line 360)
In LAB04ex3 (line 5)
This seems to mean that the solution to this equation is at approx. 4.612831e+00. This example looks
different from that of (7) because this one has a solution that the graph to stop while (7) is continuous.
7
EXERSIZE 4
function LAB04ex4
t0 = 0,tf =55; y0 =[-0.5;0.5;0];
[t,Y] = ode45(@f,[t0,tf],y0);
y = Y(:,1);
v = Y(:,2);
w = Y(:,3);
figure(1);
plot(t,y,t,v,t,w,'Linewidth',2);axis square; xlabel('t');
grid on;
legend('y(t)','v(t)','w(t)');
ylim([-3.9,3.9]);
figure(2); plot3(y,w,v);
hold on; view([-40,60]);
axis square;
grid on;
xlabel('y');ylabel('v=y''');zlabel('w = y''''');
ylim([-3,3]);
xlim([-3,3]);
zlim([-10,10]);
end
%----------------------------------------------------------------------
function dydt = f(t,Y)
y = Y(1);v = Y(2); w =Y(3);
dydt = [v;w; 7*cos(t)-5*y.^2*w-10*y*v.^2-4*v];
end
8
Powered by TCPDF (www.tcpdf.org)
Students also viewed