- #1
kishtik
- 100
- 0
Hello,
For Euler's Method of Numerical Approximations, my book (Boyce&DiPrima)
gives this algorithm:
Step 1: define f(t,y)
Step 2: input initial values t0 and y0
Step 3: input step size h and number of steps n
Step 4: output t0 and y0
Step 5: for j from 1 to n do
Step 6: k1 = f(t,y)
y = y + h*k1
t = t + h
Step 7: output t and y
Step 8: end
I tried this on Matlab. My code was:
t=input('Enter t0:');
y=input('Enter y0:');
h=input('Enter step size h:');
n=input('Enter number of steps n:');
k1=input('Define f(t,y):');
result=zeros(n,2);
result(1)=t;
result(1,2)=y;
for j=[2:n+1]
k=k1;
y=y+k*h;
t=t+h;
result(j,1)=t;
result(j,2)=y;
end
disp(' t y');
disp(result);
But I realize that my program calculates k1 only for the initial values... How can I change it so that k1 is actually calculated in every turn of for loop?
Thanks.
For Euler's Method of Numerical Approximations, my book (Boyce&DiPrima)
gives this algorithm:
Step 1: define f(t,y)
Step 2: input initial values t0 and y0
Step 3: input step size h and number of steps n
Step 4: output t0 and y0
Step 5: for j from 1 to n do
Step 6: k1 = f(t,y)
y = y + h*k1
t = t + h
Step 7: output t and y
Step 8: end
I tried this on Matlab. My code was:
t=input('Enter t0:');
y=input('Enter y0:');
h=input('Enter step size h:');
n=input('Enter number of steps n:');
k1=input('Define f(t,y):');
result=zeros(n,2);
result(1)=t;
result(1,2)=y;
for j=[2:n+1]
k=k1;
y=y+k*h;
t=t+h;
result(j,1)=t;
result(j,2)=y;
end
disp(' t y');
disp(result);
But I realize that my program calculates k1 only for the initial values... How can I change it so that k1 is actually calculated in every turn of for loop?
Thanks.
Last edited: