MATLAB Answers

Reducing 2nd order ODE into coupled ODE. Solve using Euler Method and graph.

5 ビュー (過去 30 日間)
Andres Cardenas
Andres Cardenas 2019 年 12 月 9 日
コメント済み: Andres Cardenas 2019 年 12 月 9 日
Hello, I'm trying to get a numerical solution for the given problem but I can't find a way to get it to work.
t0 = 0;
t = 5;
h = 0.1;
N = (t-t0)/h;
T = [t0:h:t;]';
Y = zeros(size(t));
Y(1) = 3;
%Start of Euler Method
syms y(t)
E = diff(y,2) + .1*diff(y) + .3*y == .02*y^3;
V = odeToVectorField(E)
for i = 1: N
P = (V(2))
Y(i+1) = Y(i) + h*P(i)
S = Y(i+1)
end
plot(T,Y,'o')
the error I'm getting is the following:
The following error occurred converting from sym to double:
Unable to convert expression into double array.
Error in Euler (line 19)
Y(i+1) = Y(i) + h*P(i)

  0 件のコメント

サインイン to comment.

回答 (1 件)

Stephan
Stephan 2019 年 12 月 9 日
tspan = [0 5]; % time span to integrate
y0 = [3 0]; % initial conditions
%Start of Euler Method
syms y(t)
E = diff(y,2) + .1*diff(y) + .3*y == .02*y^3;
V = odeToVectorField(E);
odefun = matlabFunction(V,'Vars',{'t','Y'});
[t,y] = ode45(odefun,tspan,y0);
plot(t,y,'o')

  3 件のコメント

Andres Cardenas
Andres Cardenas 2019 年 12 月 9 日
Thank you, i do need the ODE 45. What about the Euler method? I still can't get it to work. Can you see what the problem is?
Stephan
Stephan 2019 年 12 月 9 日
The result of odeToVectorField is a symbolic expression. This can not be used in the way you want it.
Andres Cardenas
Andres Cardenas 2019 年 12 月 9 日
Ok, good to know. How can I make my ODE into an actual expression that I can use for Euler's? Thank you so much for helping me.

サインイン to comment.

サインイン してこの質問に回答します。

タグ


Translated by