I'm trying to solve a system of differential equations using Euler's method, but I don't know what the problem is with my code. If you can correct me the problem

2 views (last 30 days)
clear
clc
clf
a= 0;
b= 2;
delta_x =0.5;
y1 (1) = 4;
y2 (1) = 6;
n= (b-a)/delta_x + 1 ;
x (1) = 0;
funcl =@(yl) - 0.5*yl ;
func2=@(y1, y2) 4 - 0.3* y2 - 0.1* yl ;
for i= 1:n-1
x(i+1)= i* delta_x;
y1 (i+1)= y1 (i) + delta_x * feval (funcl, yl (i));
y2 (i+1)= y2 (i)+ delta_x * feval (func2, y1 (i), y2 (i));
end
x
y1
y2
plot (x, y1, x, y2)
title('Plot of x va yi & y2 - Explicit Euler')
grid on

Accepted Answer

Alan Stevens
Alan Stevens on 24 Nov 2022
Don't confuse the number 1 with lower case l
a= 0;
b= 2;
delta_x =0.5;
y1(1) = 4;
y2(1) = 6;
n= (b-a)/delta_x + 1 ;
x(1) = 0;
func1 =@(yl) - 0.5*yl ;
func2=@(y1, y2) 4 - 0.3* y2 - 0.1* y1 ;
for i= 1:n-1
x(i+1)= i* delta_x;
y1(i+1)= y1(i) + delta_x * feval(func1,y1(i));
y2(i+1)= y2(i)+ delta_x * feval(func2, y1(i),y2(i));
end
plot (x, y1, x, y2)
title('Plot of x va yi & y2 - Explicit Euler')
grid on
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Statics and Dynamics 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!