Ask user for 3 equations and solve ODE

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);

Réponses (1)

% 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 commentaires

Torsten
Torsten le 1 Avr 2022
The question was about how to input these functions from extern, I guess.
Chris Horne
Chris Horne le 4 Avr 2022
Yes, Torsten you are corrent in this assumption
Chris Horne
Chris Horne le 4 Avr 2022
VBBV, Thanks for the tip. I need to be able to ask the user for any function of one time variable, actually 3 different functions and pass them to the loop. Your code, albeit 'fly', does not ask for user input.
Torsten
Torsten le 4 Avr 2022
... although for practical application, it's of course nonsense to ask the user to enter a function.
Quite a lot to enter usually :-)
I got then user input to work now I have symbolic double precision error: Any hints?
Unable to perform assignment because value of type 'sym' is not convertible to
'double'. Error in eulertest2 (line 20)
u(i+1,:) = u(i,:) + h*F(t(i),u(i,:)); % Euler's formula for a vector function F
Caused by: Error using symengine. Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.
% 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
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
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
v1=input('Enter the first vector fuction of time u1(t): ','s');
v2=input('Enter the first vector fuction of time u2(t): ', 's');
v3=input('Enter the first vector fuction of time u3(t): ', 's');
u1 = str2sym(v1);
u2 = str2sym(v2);
u3 = str2sym(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
Torsten
Torsten le 4 Avr 2022
Why symbolic ? Getting a function handle from the string is much better suited.
VBBV
VBBV le 5 Avr 2022
Modifié(e) : VBBV le 5 Avr 2022
Your code, albeit 'fly', does not ask for user input. It can be made to ask user input like in your code and when prompted to enter equations, user can enter the equations using function handle as shown in my code. Since i am using web verision of matlab, it does not allow GUI based or other interactive user input functons to run
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

Connectez-vous pour commenter.

Catégories

En savoir plus sur Numerical Integration and Differential Equations dans Centre d'aide et File Exchange

Produits

Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by