필터 지우기
필터 지우기

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

조회 수: 1 (최근 30일)
Chris Horne
Chris Horne 2022년 3월 23일
답변: Chris Horne 2022년 3월 31일
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일
  댓글 수: 10
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일
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

카테고리

Help CenterFile Exchange에서 Ordinary Differential Equations에 대해 자세히 알아보기

태그

제품


릴리스

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by