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

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;

 採用された回答

Torsten
Torsten 2022 年 3 月 23 日

0 投票

10 件のコメント

Chris Horne
Chris Horne 2022 年 3 月 23 日
Thanks.
Chris Horne
Chris Horne 2022 年 3 月 23 日
I added the input command and a vector of initial conditons and recieved this error:
% solve u'(t) = F(t,u(t)) where u(t)= -2t^3 + 12t^2- 20t + 9 using euler method
% Euler's Method for a 3D vector
% 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 u ?????
v=input('Enter the intial vector of 3 components using brackets')
u(1,:)= v; % the initial u value
Unable to perform assignment because the size of the left side is 1-by-14 and the size of the right side is 1-by-3.
Error in Euler2 (line 9)
u(1,:)= v; % the initial u value
Torsten
Torsten 2022 年 3 月 23 日
編集済み: Torsten 2022 年 3 月 23 日
How did you get u as 1x14 ?
If you used the hint, u should be initialized as 14x3 :
u = zeros(size(t,2),3)
Chris Horne
Chris Horne 2022 年 3 月 23 日
I got the vector dimensions to agree. Thanks. I am learning Matlab. Now in the loop I get another error that the index exceeds the number of outputs, u. I am not sure this method will solve more than one u(t) since u is a VECTOR function (3D).
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,2),3); % allocate the result u
v=input('Enter the intial vector of 3 components using brackets')
u(1,:)= v; % the initial u value
n=numel(u); % the number of u values
% The loop to solve the ODE
for i = 1:n-1
dudt= -2*t(i).^3 +12*t(i).^2 -20*t(i)+8.5 ; %the expression for u' in the ODE
u(i+1,:) = u(i,:)+dudt*h ;
Index exceeds the number of array elements (9)
Error in Euler2 (next to last line)
dudt= -2*t(i).^3 +12*t(i).^2 -20*t(i)+8.5 ; %the expression for u' in the ODE
Torsten
Torsten 2022 年 3 月 23 日
編集済み: Torsten 2022 年 3 月 23 日
dudt is a scalar. It must be an 1x3 vector because you wanted to solve 3 ODEs simultaneously.
So
neqn = 3;
h = 0.01;
t = (0:h:1).';
nt = size(t,1);
f = @(t,u) [-2*t^3+12*t^2-20*t+8.5,-2*t^3+12*t^2-20*t+8.5,-2*t^3+12*t^2-20*t+8.5];
u = zeros(nt,neqn);
u(1,:) = [1, 3, -4];
for i = 1:nt-1
u(i+1,:) = u(i,:) + h*f(t(i),u(i,:));
end
plot(t,u(:,1))
Chris Horne
Chris Horne 2022 年 3 月 23 日
Thanks again.
Chris Horne
Chris Horne 2022 年 3 月 23 日
What if I want to plot each of the 3 outputs, u1(t), u2(t) , and u3(t) on the same Matlab figure. plot(t, ??)
Chris Horne
Chris Horne 2022 年 3 月 23 日
Like this to plot 3 vector functions which are solution to the vector ODE?
plot(t,u(:,1),'color','r')% plot the first column vector of function values
hold on;
plot(t,u(:,2),'color','g')% plot the second column vector of function values
hold on;
plot(t,u(:,3),'color','b') % plot the third column vector of function values
hold off
title('Euler Method with Vector Functions')
Torsten
Torsten 2022 年 3 月 23 日
編集済み: Torsten 2022 年 3 月 23 日
Yes.
Or:
plot(t,y,'Color', 'rgb')
Chris Horne
Chris Horne 2022 年 3 月 24 日
TNX which is morse code language for Thanks

サインインしてコメントする。

その他の回答 (1 件)

Chris Horne
Chris Horne 2022 年 3 月 31 日

0 投票

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

カテゴリ

ヘルプ センター および File ExchangeNumerical Integration and Differential Equations についてさらに検索

製品

リリース

R2021b

タグ

質問済み:

2022 年 3 月 23 日

回答済み:

2022 年 3 月 31 日

Community Treasure Hunt

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

Start Hunting!

Translated by