Effacer les filtres
Effacer les filtres

How to specify a 3 element column vector in Euler's Method for ODE

1 vue (au cours des 30 derniers jours)
Chris Horne
Chris Horne le 23 Mar 2022
I am writing code that will approximate the solution to an ODE IVP. I want the initual condition to be a 3D column array supplied by the user rather than one number becuase I am simulating a 3D vector , u(t) that changes in time. I am unsure how to make this initial condition 3D vector.
% u'(t) = F(t, u(t)) where u(t) = t^3 + 12t^2- 20t +1
% u(0) = v % note v is a vector
% solve du/dt = t^3 + 12t^2- 20t + 1 using euler method
% Euler's Method
% Initial conditions and setup
h=input('Enter the step size') % step size
t=0:h:4;%(starting time value 0):h step size
%(the ending value of t3 ); % the range of t
u=zeros(size(t)); % allocate the result y
%v=input('Enter the intial vector of 3 components using brackets') ??????????
u(1,1,1)=v; % the initial u as 3D. I GET ERROR AT THIS LINE
n=numel(u); % the number of u values
% The loop to solve the ODE
for i = 1:n-1
dudt= *t(i).^3 +12*t(i).^2 -20*t(i)+1 ; %the expression for u' in the ODE
u(i+1) = u(i)+dudt*h ;
fprintf('="Y"\n\t %0.01f',u(i));
end
%%fprintf('="U"\n\t %0.01f',u);
plot(t,u);
xlabel('t')
ylabel('u(t)')
grid on;

Réponse acceptée

Torsten
Torsten le 23 Mar 2022
  10 commentaires
Torsten
Torsten le 23 Mar 2022
Modifié(e) : Torsten le 23 Mar 2022
Yes.
Or:
plot(t,y,'Color', 'rgb')
Chris Horne
Chris Horne le 24 Mar 2022
TNX which is morse code language for Thanks

Connectez-vous pour commenter.

Plus de réponses (1)

Chris Horne
Chris Horne le 31 Mar 2022
Is the term 'forward Euler' the same as 'Euler' in terms of the algorithm? Here is my method for solving 3 equaitons as a vector:
% This code solves u'(t) = F(t,u(t)) where u(t)= t, cos(t), sin(t)
% using the FORWARD EULER METHOD
% Initial conditions and setup
neqn = 3; % set a number of equations variable
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
%(the ending value of t ); % the range of t
% define the function vector, F
F = @(t,u)[t,cos(t),sin(t)]; % define the function 'handle', F
% 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 and the first column
%n=numel(u); % the number of u values
% The loop to solve the ODE (Forward Euler Algorithm)
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

Produits


Version

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by