Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Exercise 1 :

Code :
clc;
clear all;
close all;
x0=0;
x1=3;
x = [];
y = [];
h=1/16;
y0=1;

while x0 <= x1
x = [x x0];
y = [y y0];
y2 = y0 + ((x0-y0)/2)*h;
y0 = y2;
x0 = x0 + h;
end
plot (x,y,x,(3*exp(-x/2)-2+x));
print('pic1','-dpng');

Solution :
Exercise 2 :
Code :
clc;
clear all;
close all;
t0 = 0;
t1 = 5;
t = [];
x = [];
h = 0.01;
x0 = 3;
p0 = -5;

while t0 <= t1
t = [t t0];
x = [x x0];
p2 = p0 + ((-4*p0) - (5*x0))*h;
x2=x0+p2*h;
p0=p2;
x0 = x2;
t0 = t0 + h;
end
plot (t,x);
print('pic2','-dpng');
Solution :
Exercise 3 :
Code :
clc;
clear all;
close all;
t0 = 0;
T0 = 0;
t1 = 5;
t = [];
T = [];
u = [];
v=[];
h = 0.01;
x0 = 3;
x1 = -2.9425e-05;
u0=3;
p0=0;
v0=0;
q0=1;

while t0 <= t1
t = [t t0];
u = [u u0];
p2 = p0 + ((-4*p0) - (5*u0))*h;
u2=u0+p2*h;
p0=p2;
u0 = u2;
t0 = t0 + h;
end
plot (t,u)
hold on;
while T0 <= t1
T = [T T0];
v = [v v0];
q2 = q0 + ((-4*q0) - (5*v0))*h;
v2=v0+q2*h;
q0=q2;
v0 = v2;
T0 = T0 + h;
end
plot(T,v)
c = (x1-u(length(u)))/v(length(v));

x=u+c.*v;

plot(t,x,'r');
print('pic3','-dpng');

Solution :

You might also like