Ask user for 3 equations and solve ODE
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
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);
0 Commenti
Risposte (1)
VBBV
il 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]
8 Commenti
VBBV
il 5 Apr 2022
Modificato: VBBV
il 5 Apr 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
Vedere anche
Categorie
Scopri di più su Numerical Integration and Differential Equations in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!