Can anyone help me with a code for solving a differential equation using Euler\'
ID: 3551211 • Letter: C
Question
Can anyone help me with a code for solving a differential equation using Euler's method in Matlab?
I went on a couple of websites but none seemed to help. I have part one, done, I just need help on the second part (boxed in red).
Explanation / Answer
%y' = 0.5*(y+1/y)(t+1)
k=2;
u(1)=1;
%case1 h=0.2
t=0;
h=0.2;
for i=1:10
t=t+h;
u(k) = u(k-1)+ h * (0.5*(u(k-1)+1/u(k-1))*(t+1));
k=k+1;
end
x=sprintf('at x=0.4,%f at x=0.8, %f at x=1.2, %f at x=1.6, %f at x=2, %f ',u(2),u(4),u(6),u(8),u(10));
disp(x);
k=2;
u(1)=1;
%case1 h=0.2
t=0;
h=0.2;
h=0.1;
for i=1:20
t=t+h;
u(k) = u(k-1)+ h * (0.5*(u(k-1)+1/u(k-1))*(t+1));
k=k+1;
end
x=sprintf('at x=0.4,%f at x=0.8, %f at x=1.2, %f at x=1.6, %f at x=2, %f ',u(4),u(8),u(12),u(16),u(20));
disp(x);
k=2;
u(1)=1;
%case1 h=0.2
t=0;
h=0.2;
h=0.05;
for i=1:40
t=t+h;
u(k) = u(k-1)+ h * (0.5*(u(k-1)+1/u(k-1))*(t+1));
k=k+1;
end
x=sprintf('at x=0.4,%f at x=0.8, %f at x=1.2, %f at x=1.6, %f at x=2, %f ',u(8),u(16),u(24),u(32),u(40));
disp(x);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.