Help with user defined function to solve an ODE with modified euler predict-corrector method

10 views (last 30 days)
Hi, I need to find the [t,y] values for the ODE dy/dt=t^2-3*y/t in the range of t=[1-2.2] with IC: [t1,y1]=[1,1]
for far my code is:
clear all; close all; clc;
dy_dt=@(t, y) t.^2-3*y/t;
t(1)=1;
y(1)=1;
yfinal(1)=1;
h=0.1;
no_points=(2.2-1)/h +1;
%% Loop from 2nd point to last point and apply Euler's steps
for i=2:no_points
t(i)=t(i-1)+h; % Calculate x value of the current point
slope(i-1)=dy_dt(t(i-1),y(i-1)); % Calculate slope at previous point
y(i)=y(i-1)+slope(i-1)*h; % Calculate y value of the current point
j=1;
y(j)=y(i);
for j=2:100 %attempt at modified method, with predictor-corrector
slopemod(j-1)=slope(i-1)+dy_dt(t(i),y(i));
ymodeu(j)=y(i-1)+slopemod(j-1)*h*0.5;
Tol(j)=abs((ymodeu(j)-y(j-1))/y(j-1));
if Tol(j) < 0.0005
yfinal(i)=ymodeu(j) % Save the Solution
t(i)
break;
else
y(i)=ymodeu(j);
end
end
end
I feel like the logic follows but it just doesn't work... please help

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!