Ask user for 3 equations and solve ODE

2 views (last 30 days)
Chris Horne
Chris Horne on 1 Apr 2022
Commented: Chris Horne on 5 Apr 2022
I am trying to use the input command to ask user for 3 differential equations in the form, u'(t) = F [ t, u(t) ] . The right hand side vector functions, the RHS of u'(t). I want to 'pass' these functions to the loop in solving euler method. I think my format for the f1,f2 anf f3 is wrong since the error is in the loop. Any help is appreciated.
% solve u'(t) = F(t,u(t)) where u = [u1(t), u2(t), u3(t)]'
% using forward euler method
% Initial conditions and setup
neqn = 3; % set a number of equations
f1=input('Enter the first equation such as cos(t)');
f2=input('Enter the second equation such as 2*t');
f3=input('Enter the third equation');
h=input('Enter the step size: ') % step size will effect solution size
t=(0:h:4).';%(starting time value 0):h step size
nt = size(t,1); % size of time array
% define the function vector, F
F = @(t,u)[f1,f2,f3]; % define the function 'handle', F ???????? ERROR using this syntax
% with hard coded vector functions of time
u = zeros(nt,neqn); % initialize the u vector with zeros
v=input('Enter the intial vector values of 3 components using brackets [u1(0),u2(0),u3(0)]: ')
u(1,:)= v; % the initial u value amd the first column
%n=numel(u); % the number of u values
% The loop to solve the ODE (Euler's Method)
for i = 1:nt-1
u(i+1,:) = u(i,:) + h*F(t(i),u(i,:)); % Euler's formula for a vector function F
end
fprintf('="U"\n\t %0.01f',u);

Answers (1)

VBBV
VBBV on 1 Apr 2022
% solve u'(t) = F(t,u(t)) where u = [u1(t), u2(t), u3(t)]'
% using forward euler method
% Initial conditions and setup
neqn = 3; % set a number of equations
f1=@(t) cos(t);%input('Enter the first equation such as cos(t)');
f2=@(t) 2*t; %input('Enter the second equation such as 2*t');
f3=@(u) 4*u;%input('Enter the third equation');
h=0.1;%input('Enter the step size: ') % step size will effect solution size
tt=(0:h:4).';%(starting time value 0):h step size
nt = size(tt,1); % size of time array
u1 = zeros(nt,neqn); % initialize the u vector with zeros
u2 = zeros(nt,neqn); % initialize the u vector with zeros
u3 = zeros(nt,neqn); % initialize the u vector with zeros
v=[0 1 2];%input('Enter the intial vector values of 3 components using brackets [u1(0),u2(0),u3(0)]: ')
u1(1,:)= v; % the initial u value amd the first column
u2(1,:)= v; % the initial u value amd the first column
u3(1,:)= v; % the initial u value amd the first column
%n=numel(u); % the number of u values
% The loop to solve the ODE (Euler's Method)
for i = 1:nt-1
u1(i+1,:) = u1(i,:) + h*f1(tt(i)); % Euler's formula for a vector function F
u2(i+1,:) = u2(i,:) + h*f2(tt(i)); % Euler's formula for a vector function F
u3(i+1,:) = u3(i,:) + h*f3(tt(i)); % Euler's formula for a vector function F
end
u = [u1 u2 u3]
u = 41×9
0 1.0000 2.0000 0 1.0000 2.0000 0 1.0000 2.0000 0.1000 1.1000 2.1000 0 1.0000 2.0000 0 1.0000 2.0000 0.1995 1.1995 2.1995 0.0200 1.0200 2.0200 0.0400 1.0400 2.0400 0.2975 1.2975 2.2975 0.0600 1.0600 2.0600 0.1200 1.1200 2.1200 0.3930 1.3930 2.3930 0.1200 1.1200 2.1200 0.2400 1.2400 2.2400 0.4851 1.4851 2.4851 0.2000 1.2000 2.2000 0.4000 1.4000 2.4000 0.5729 1.5729 2.5729 0.3000 1.3000 2.3000 0.6000 1.6000 2.6000 0.6554 1.6554 2.6554 0.4200 1.4200 2.4200 0.8400 1.8400 2.8400 0.7319 1.7319 2.7319 0.5600 1.5600 2.5600 1.1200 2.1200 3.1200 0.8016 1.8016 2.8016 0.7200 1.7200 2.7200 1.4400 2.4400 3.4400
  8 Comments
Chris Horne
Chris Horne on 5 Apr 2022
Thanks Torsten. I used str2func. I'm getting this error for the F handle: "Nonscalar arrays of function handles are not allowed; use cell arrays instead."
u1 = str2func(v1);
u2 = str2func(v2);
u3 = str2func(v3);
F = @(t,u) [u1,u2,u3]; % define the function 'handle', F
% The loop to solve using Euler's Method
for i = 1:nt-1
u(i+1,:) = u(i,:) + h*F(t(i),u(i,:)); % Euler's formula for a vector function F
end

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by