Euler's Method/Improved Euler's Method

Having trouble working out the bugs in my Improved Euler's Method code. I previously had trouble with the normal Euler's method code, but I figured it out.
Euler's Method (working code):
syms t y
h=0.01;
N=200;
y(1)=1;
t(1)=0;
for n=1:N
k1=1-t(n)+4*y(n);
y(n+1)=y(n)+h*k1;
t(n+1)=t(n)+h;
end
plot(t,y)
And here is my attempt at Improved Euler's Method:
h=0.01;
N=200;
y(1)=1;
t(1)=0;
for n=1:N
k1=1-t(n)+4*y(n);
k2=1-t(n+1)+4*(y(n)+h*k1);
y(n+1)=y(n)+(h/2)*(k1+k2);
t(n+1)=t(n)+h;
end
plot(t,y)
The error message that pops up is "Index exceeds the number of array elements (1)." I'm rather new at MATLAB, and don't know what this means, can someone help me rework this? Thank you!

2 件のコメント

Lucas Howarth
Lucas Howarth 2020 年 10 月 9 日
Here is the initial value problem: y'=1-t+4*y with y(0)=1 on the interval [0, 2] using a step size of h = 0.01
Mike Asmanis
Mike Asmanis 2021 年 6 月 18 日
Hey , how would i be able to solve this : y'(t)=cos(t + y) y(0)=0 t[0,3] exact solution y(t)=-t + 2arctan(t)
using your code?

サインインしてコメントする。

 採用された回答

Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 9 日

1 投票

May be position of t(n+1)=t(n)+h; coulb be at the starting of loop.

6 件のコメント

Lucas Howarth
Lucas Howarth 2020 年 10 月 9 日
Thank you so much!! That worked for me!
Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 9 日
Welcome. Glad to help you.
Lucas Howarth
Lucas Howarth 2020 年 10 月 9 日
Sorry to bother you again, but now I have to do Runge-Kutta Method on the same ODE (step size is now h=0.1) and I'm getting the error "Array indices must be positive integers or logical values." I thought that I used similar formatting as I did in the Improved Euler problem, so I'm not sure what the issue is.
Here's my code:
clear all
close all
clc
h=0.1;
N=20;
y(1)=1;
t(1)=0;
for n=1:N
t(n+1)=t(n)+h;
k1=1-t(n)+4*y(n);
k2=1-t(n+0.5)+4*(y(n)+0.5*h*k1);
k3=1-t(n+0.5)+4*(y(n)+0.5*h*k2);
k4=1-t(n+1)+4*(y(n)+h*k3);
y(n+1)=y(n)+(h/6)*(k1+k2+k3+k4);
end
plot(t,y)
Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 9 日
Is any specific reason to use t(n+0.5)?
I am not sure about mathematical equation but if t(n+0.5) can be replaced with t(n+1), your error will get resolved.
Lucas Howarth
Lucas Howarth 2020 年 10 月 9 日
For the Runge-Kutta Method for approximation, k2 and k3 are done with the "t" value halfway between the current step and the next step. I'm not sure how to do this in MATLAB and still keeping integer values.

サインインしてコメントする。

その他の回答 (1 件)

J. Alex Lee
J. Alex Lee 2020 年 10 月 9 日

0 投票

The error is telling you that at the first step of your loop (n=1), you are trying to access the n=2nd element of t and y, but at the stage, t and y are only scalars (arrays with only 1 element) variables. You are trying to access an element of the "arrays" that doesn't exist.
if you are trying to implement implicit Euler, your problem is math, not coding.

2 件のコメント

J. Alex Lee
J. Alex Lee 2020 年 10 月 9 日
my bad, i didn't look very closely. i guess you are doing a 2 step RK, and it is probably right according to Sudhakar's answer.
Sudhakar Shinde
Sudhakar Shinde 2020 年 10 月 9 日
yup.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeMathematics についてさらに検索

製品

リリース

R2020a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by