Question

In: Computer Science

Using Euler method, ODE45 and Simulink solve the following ODE's. a) y+c(dy/dt)=10sinwt c=2, w=10, y(0)=0 Assume...

Using Euler method, ODE45 and Simulink solve the following ODE's.
a) y+c(dy/dt)=10sinwt
c=2, w=10, y(0)=0
Assume time span 0: 0.01: 10
b) dy/dt=t^2+yz
dz/dt=t+y^2z^2
y(0)=0.2
z(0)=-0.1
Assume time span 0: 0.1 :1
c)(d^2)y/dt^2+0.5(dy/dt)+siny=0
y(0)=1
dy/dt =0 when t=0
assume time span 0: 0.01 :10
Matlab code please

Solutions

Expert Solution

% PART (a):
% y + c dy/dt = 10 sin wt, y(0) = 0
% dy/dt = (10 sin wt - y) / c

clc,clear,close all

c = 2; w = 10; y0 = 0;
h = 0.01; % dt
t = 0 : h : 10; % time vector (span)
n = length(t) - 1; % number of sub-intervals

f = @(t,y) (10 * sin(w*t) - y) / c; % dy/dt as a function

% Euler's method
y(1) = y0; % set initial condition
for i = 1 : n
y(i+1) = y(i) + h * f(t(i),y(i));
end

% ode45 method
[~,y2] = ode45(f,t,y0);

% plot results
plot(t,y,'- r',t,y2,'- b')
xlabel('t'),ylabel('y(t)'),legend('euler','ode45')


% PART (b): dy/dt = t^2 + yz, y(0)=0.2, dz/dt = t + y^2z^2, z(0)=-0.1
clc,clear,close all

h = 0.1; t0 = 0; tn = 1;
t = t0 : h : tn;
y0 = 0.2; z0 = -0.1; % y(0) and z(0)
% write the odes as functions f(t,p) where p = [y,z] ie. y = p(1), z = p(2)
f1 = @(t,p) t^2 + p(1)*p(2); % dy/dt = t^2 + yz
f2 = @(t,p) t + p(1)^2 * p(2)^2; % dz/dt = t + y^2z^2

% Euler's method
y(1) = y0; z(1) = z0; % set the initial conditions
n = (tn - t0)/h;
for i = 1 : n
y(i+1) = y(i) + h * f1(t(i),[y(i) z(i)]);
z(i+1) = z(i) + h * f2(t(i),[y(i) z(i)]);
end

% ode45 method
f = @(t,p) [f1(t,p) ; f2(t,p)];
[~,P] = ode45(f,t,[y0 z0]);
y2 = P(:,1); % y solution
z2 = P(:,2); % z solution

% plot results
figure,plot(t,y,'-r',t,y2,'-b'),xlabel('t'),ylabel('y'),legend('euler','ode45')
figure,plot(t,z,'-r',t,z2,'-b'),xlabel('t'),ylabel('z'),legend('euler','ode45')


% PART (c):
% d^y/dt^2 + 0.5 dy/dt + sin y = 0, y(0) = 1, dy/dt(0) = 0
% y'' + 0.5y' + sin y = 0, y(0) = 1, y'(0) = 0
% let z = y'
% z' + 0.5z + sin y = 0, y(0) = 1, z(0) = y'(0) = 0
% z' = -0.5z - sin y
% solve the 2 odes simulteneously:
% y' = z, y(0) = 1
% z' = -0.5z - sin y, z(0) = 0


clc,clear,close all

t0 = 0; tn = 10;
h = 0.01;
t = t0 : h : tn; % time vector

% initial conditions y(0) and z(0)
y0 = 1; z0 = 0;   

% define the odes as functions f1(t,p) and f2(t,p) where p = [y,z]
f1 = @(t,p) p(2); % y' = z
f2 = @(t,p) -0.5*p(2) - sin(p(1)); % z' = -0.5z - sin y

% Euler's method
y(1) = y0; z(1) = z0; % set the initial conditions
n = length(t) - 1; % number of sub-intervals

for i = 1 : n
y(i+1) = y(i) + h * f1(t(i),[y(i) z(i)]);
z(i+1) = z(i) + h * f2(t(i),[y(i) z(i)]);
end

% ode45 method   
f = @(t,p) [ f1(t,p) ; f2(t,p)];   
[~,P] = ode45(f,t,[y0 z0]);
% extract solutions
y2 = P(:,1); % y solution

% plot results
figure,plot(t,y,'-r',t,y2,'-b')
xlabel('t'),ylabel('y(t)'),legend('euler','ode45')


------------------------------------------------------------------------------
COMMENT DOWN FOR ANY QUERY RELATED TO THIS ANSWER,

IF YOU'RE SATISFIED, GIVE A THUMBS UP
~yc~


Related Solutions

Use Euler method (as explained on the white board) to solve numerically the following ODE: dy/dt=y+t....
Use Euler method (as explained on the white board) to solve numerically the following ODE: dy/dt=y+t. y(0)=1 You can select the step size. Choose n=3. solve this by hand on a paper and with the aid of Matlab.
Use a LaPlace transform to solve d^2x/dt^2+dx/dt+dy/dt=0 d^2y/dt^2+dy/dt-4dy/dt=0 x(0)=1,x'(0)=0 y(0)=-1,y'(0)=5
Use a LaPlace transform to solve d^2x/dt^2+dx/dt+dy/dt=0 d^2y/dt^2+dy/dt-4dy/dt=0 x(0)=1,x'(0)=0 y(0)=-1,y'(0)=5
Solve the system of equation by method of elimination. dx/dt + x−5y = 0, 4x +dy/dt+...
Solve the system of equation by method of elimination. dx/dt + x−5y = 0, 4x +dy/dt+ 5y = 0, x(0) = −1, y(0) = 2.
Solve the following initial value problems (1) dy/dt = t + y y(0) = 1 so...
Solve the following initial value problems (1) dy/dt = t + y y(0) = 1 so y(t) = (2)  dy/dt = ty y(0) = 1 so y(t) =
Solve the given initial-value problem. dx/dt = y − 1 dy/dt = −6x + 2y x(0)...
Solve the given initial-value problem. dx/dt = y − 1 dy/dt = −6x + 2y x(0) = 0, y(0) = 0
Consider the following initial value problem dy/dt = 3 − 2*t − 0.5*y, y (0) =...
Consider the following initial value problem dy/dt = 3 − 2*t − 0.5*y, y (0) = 1 We would like to find an approximation solution with the step size h = 0.05. What is the approximation of y(0.1)?
y' = 2 + t^2 + y^2 0<t<1 y(0)=0 use the euler method to determine step...
y' = 2 + t^2 + y^2 0<t<1 y(0)=0 use the euler method to determine step size (h) to keep global truncation error below .0001
solve using laPlace dy/dt+4y=40sin3t; y(0)=6 Please show all steps and write neat, thanks!
solve using laPlace dy/dt+4y=40sin3t; y(0)=6 Please show all steps and write neat, thanks!
dx dt =ax+by dy dt =−x − y, 2. As the values of a and b...
dx dt =ax+by dy dt =−x − y, 2. As the values of a and b are changed so that the point (a,b) moves from one region to another, the type of the linear system changes, that is, a bifurcation occurs. Which of these bifurcations is important for the long-term behavior of solutions? Which of these bifurcations corresponds to a dramatic change in the phase plane or the x(t)and y(t)-graphs?
Solve (1+e^x)dy/dx+(e^x)y=3x^2+1 Solve (x^3+y^3)dx+3xy^2 dy = 0 Solve (y-cos y)dx + (xsiny+x)dy = 0 Solve (1+ln...
Solve (1+e^x)dy/dx+(e^x)y=3x^2+1 Solve (x^3+y^3)dx+3xy^2 dy = 0 Solve (y-cos y)dx + (xsiny+x)dy = 0 Solve (1+ln x +y/x)dx = (1-lnx)dy Solve (y^2+yx)dx - x^2dy =0 Solve (x^2+2y^2)dx = xydy Solve Bernoulli's Equation x dy/dx + 2y = (x^4)(e^x)(y^2) Solve Bernoulli's Equation (1+x^2) dy/dx = 2xy +(e^x)(y^2) Solve IVP (3e^(x^2))dy + (xy^2)dx=0 ; y(1) = 2 Solve IVP dy/dx -2xy = e^(x^2) ; y(0)=0 Solve IVP (x^2+y^2)dx+(2xy)dy=0; y(1)=1 6. Mixture Problem Initially 40 lb of salt is dissolved in a large...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT