problem with ode45 solving
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
0 投票
Hello, I'm trying to solve the following system of differential equations but I'm not getting the right answer
ode1 = diff(x) == v;
ode2 = diff(v) == u;
t_interval = [0,10]
x0 = 0
v0 = 0
u = 1
because of how things are set up I can wrap my head on creating the function. Any help would be appreciate it.
採用された回答
Star Strider
2021 年 4 月 28 日
Assuming both functions are functions of time —
syms x(t) v(t)
u = sym(1);
ode1 = diff(x) == v
ode1(t) =

ode2 = diff(v) == u
ode2(t) =

[x(t),v(t)] = dsolve(ode1,ode2, x(0)==0, v(0)==0)
x(t) =
t
v(t) =

figure
fplot(x, [0 10])
hold on
fplot(v, [0 1])
hold off
grid
xlabel('t')
legend('x(t)','v(t)')

.
20 件のコメント
Juan Hurtado
2021 年 4 月 28 日
This great thanks! I was confused on the u = sym(1) and thats why I was have trouble implemented it but this should work thanks!
Juan Hurtado
2021 年 4 月 29 日
Also I want to change u for -sign(x) but when I do that I got the following error:
syms x(t) v(t)
u1 = sym(-sign(x));
%time interval and initial conditions
x0 = 0;
v0 = 0;
t_interval = [0,5];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u1;
[x(t),v(t)] = dsolve(ode1,ode2, x(0)==x0, v(0)==v0);
figure
fplot(x, t_interval)
hold on
fplot(v, t_interval)
hold off
grid
xlabel('t')
legend('x(t)','v(t)')
"
diff(v(t), t) == -sign(x(t))
Warning: Unable to find symbolic solution. "
What do you think its happening?
Star Strider
2021 年 4 月 29 日
The sign call creates a nonlinear condition so a symbolic solution is not possible. (I'm replying on my phone since my ISP is in fail mode.) Consider substituting the tanh() function for 'sign'. I cannot test that just now, so experiment to see if it does what you want.
Juan Hurtado
2021 年 4 月 29 日
Cool, thanks for your help. I tried using u = sym(-tanh(x)) and I got the same error.
Star Strider
2021 年 4 月 29 日
As always, my pleasure!
If dsolve cannot work with it, then the only option is to integrate it numerically —
syms x(t) v(t) Y
% u1 = sym(-sign(x));
u1 = -tanh(x);
%time interval and initial conditions
x0 = 0;
v0 = 0;
t_interval = [0,5];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u1;
[VF,Subs] = odeToVectorField(ode1,ode2)
VF =

Subs =

odefcn = matlabFunction(VF, 'Vars',{t,Y});
[t,y] = ode45(odefcn, [0 5], [0 0]+1E-8);
figure
plot(t,y)
grid
xlabel('t')
legend('x(t)','v(t)')

.
Juan Hurtado
2021 年 4 月 29 日
this is great thank you so much! still wondering why sign does note work as well here. I tried it and I mean you get results but it doesn't seem to make sense.
Star Strider
2021 年 4 月 29 日
As always, my pleasure! Thank you!
The problem is that the sign function creates abrupt discontinuities. Numerical integration functiond cannot handle those well becaues they are not differentiable. The tanh function does something similar, however since it is differentiable, the numeric integration succeeds.
Juan Hurtado
2021 年 4 月 29 日
ohh that makes sence thank you!
One last thing. So now I want to solve this system of equations
syms x(t) v(t) a(t)
u = sym(1);
%time interval and initial conditions
x0 = 0;
v0 = 0;
t_interval = [0,10];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == a;
ode3 = diff(a) == u;
[x(t),v(t),a(t)] = dsolve(ode1,ode2,ode3, x(0)==x0, v(0)==v0);
I got an answer but when I'm trying to plot on component I get the following (e.g plot(x, t_interval)) I get the following error "Data must be numeric, datetime, duration or an array convertible to double."
Do you know what it could be?
Star Strider
2021 年 4 月 30 日
I do not see the plot call, however I assume you used plot rather than fplot.
And, ‘a’ needs an initial condition, too.
Try this —
syms x(t) v(t) a(t)
u = sym(1);
%time interval and initial conditions
x0 = 0;
v0 = 0;
t_interval = [0,10];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == a;
ode3 = diff(a) == u;
[x(t),v(t),a(t)] = dsolve(ode1,ode2,ode3, x(0)==x0, v(0)==v0, a(0)==0)
x(t) =
t
v(t) =

a(t) =

figure
fplot(x, t_interval)
hold on
fplot(v, t_interval)
fplot(a, t_interval)
hold off
grid
legend('x(t)','v(t)','a(t)', 'Location','best', 'Interpreter','latex')

.
Juan Hurtado
2021 年 5 月 3 日
ohh cool that works! One last question that I'd have would be on what would be a better way to plot the results of the simulation across different initial conditions and plot them in the same plot.
I tried running a for loop but is not working, I'm having the following error:
syms x(t) v(t)
u = sym(1);
%time interval and initial conditions
x = [-3, -2, -1, 1, 2, 3];
v0 = 0;
t_interval = [0,5];
for i = 1:length(v)
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u;
[x(t),v(t)] = dsolve(ode1,ode2, x(0)==x(i), v(0)==v0);
figure
fplot(x, t_interval)
hold on
fplot(v, t_interval)
hold off
grid
xlabel('t')
legend('x(t)','v(t)')
end
Array indices must be positive integers or logical values.
Any help would be appreciate it.
Star Strider
2021 年 5 月 3 日
Thank you!
This was a bit of a challenge, because of the way the Symbolic Math Toolbox works. The solution was to create a new copy of ‘v(t)’ in each iteratioon (as well as rename vector ‘x’ as ‘xv’ since ‘x’ is already defined as ‘x(t)’ and defining it as a vector later that confuses things).
syms x(t) v(t) x0
u = sym(1);
%time interval and initial conditions
xv = [-3, -2, -1, 1, 2, 3];
v0 = 0;
t_interval = [0,5];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u;
[x(t),v(t)] = dsolve(ode1,ode2, x(0)==x0, v(0)==v0)
x(t) =
t
v(t) =
vx(t) = v % Copy 'v' to 'vx'
vx(t) =
for i = 1:length(xv)
v = vx % Create New Copy Of 'v' In Each Loop
ic_v = xv(i) % Display 'xv(i)'
v = subs(v,{x0},{ic_v}) % Substitute 'v(i)' For 'x0'
figure
fplot(x, t_interval)
hold on
fplot(v, t_interval)
hold off
grid
ylim([-3 16])
xlabel('t')
legend('x(t)','v(t)', 'Location','best')
end
v(t) =
ic_v = -3
v(t) =

v(t) =
ic_v = -2
v(t) =

v(t) =
ic_v = -1
v(t) =

v(t) =
ic_v = 1
v(t) =

v(t) =
ic_v = 2
v(t) =

v(t) =
ic_v = 3
v(t) =

.
Juan Hurtado
2021 年 5 月 3 日
this is really helpfull! thank you! What if I wanted to plot all of them in the same plot? I though I would have to use this command during the for loop but is not plotting together.
hold on
Star Strider
2021 年 5 月 3 日
As always, my pleasure!
Try this —
syms x(t) v(t) x0
u = sym(1);
%time interval and initial conditions
xv = [-3, -2, -1, 1, 2, 3];
v0 = 0;
t_interval = [0,5];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u;
[x(t),v(t)] = dsolve(ode1,ode2, x(0)==x0, v(0)==v0)
x(t) =
t
v(t) =
vx(t) = v; % Copy 'v' to 'vx'
figure
hold on
for i = 1:length(xv)
v = vx; % Create New Copy Of 'v' In Each Loop
ic_v = xv(i); % Display 'xv(i)'
v = subs(v,{x0},{ic_v}); % Substitute 'v(i)' For 'x0'
fplot(x, t_interval,'-k','LineWidth',1.2)
fplot(v, t_interval)
end
hold off
grid
xlabel('t')
icv = compose('v(t) i.c. = %2d',xv);
legend(['x(t)',icv], 'Location','best')

.
Juan Hurtado
2021 年 5 月 3 日
This is great! Thank you. I was trying to do the same with the sign function part:
syms x(t) v(t) Y
u1 = sym(-sign(x));
%time interval and initial conditions
xv = [-3, -2, -1, 1, 2, 3];
v0 = 0;
t_interval = [0,20];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u1;
[VF,Subs] = odeToVectorField(ode1,ode2);
[x(t),v(t)] = dsolve(ode1,ode2, x(0)==x0, v(0)==v0);
odefcn = matlabFunction(VF, 'Vars',{t,Y});
[t,y] = ode45(odefcn, t_interval, [x0 v0]+1E-8);
vx(t) = v; % Copy 'v' to 'vx'
figure
hold on
for i = 1:length(xv)
v = vx; % Create New Copy Of 'v' In Each Loop
ic_v = xv(i); % Display 'xv(i)'
v = subs(v,{x0},{ic_v}); % Substitute 'v(i)' For 'x0'
fplot(x, t_interval,'-k','LineWidth',1.2)
fplot(v, t_interval)
end
hold off
grid
xlabel('t')
icv = compose('v(t) x0 = %2d',xv);
legend(['x(t)',icv], 'Location','best')
and I get Unrecognized function or variable 'x0'. Which makes sense since now x0 is now a vector xv but I'm kind of confused on how to reeplace x0 for each of the elements in xv.
Star Strider
2021 年 5 月 3 日
Please do not mix symbolic and numeric calculations!
Try this —
syms x(t) v(t) Y x0
u1 = sym(-sign(x));
%time interval and initial conditions
xv = [-3, -2, -1, 1, 2, 3];
v0 = 0;
t_interval = [0,20];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == u1;
[VF,Subs] = odeToVectorField(ode1,ode2);
% [x(t),v(t)] = dsolve(ode1,ode2, x(0)==x0, v(0)==v0);
odefcn = matlabFunction(VF, 'Vars',{t,Y});
vx(t) = v; % Copy 'v' to 'vx'
tspan = double(t_interval);
figure
hold on
for i = 1:length(xv)
[t,y] = ode45(odefcn, tspan, [xv(i) 0]+1E-8);
plot(t,y)
end
hold off
grid
xlabel('t')
icv = compose('v(t) x0 = %2d',xv);
legend(['x(t)',icv], 'Location','bestoutside')

.
Juan Hurtado
2021 年 5 月 3 日
ohhh I see now why it wasn't working, thank you again!. I have one last question:
I'm trying to run the sign function for the 3 differential equations
syms x(t) v(t) a(t)
k = 1;
u1 = sym(-sign(x+k*v));
%time interval and initial conditions
x0 = 0;
v0 = 0;
t_interval = [0,5];
%solution
ode1 = diff(x) == v;
ode2 = diff(v) == a;
ode3 = diff(a) == u1;
[x(t),v(t),a(t)] = dsolve(ode1,ode2,ode3, x(0)==x0, v(0)==v0, a(0)==0);
figure
fplot(x, t_interval)
hold on
fplot(v, t_interval)
fplot(a, t_interval)
hold off
grid
legend('x(t)','v(t)','a(t)', 'Location','best', 'Interpreter','latex')
But I'm having an Warning: Unable to find symbolic solution. I thing I already defining of the symbolic functions but I don't know why is not working
Juan Hurtado
2021 年 5 月 3 日
Also I have x0=1
Star Strider
2021 年 5 月 3 日
The sign call creates a nonlilnear function for ‘a(t)’. Most nonlinear differential equations do not have analytic solutions, and this is one of them.
Additionally, the sign function creates a discontinuity that even the numeric solvers are going to have problems with, so create a differentiable version of ‘u1’ as:
u1 = -tanh(x+k*v);
or:
u1 = @(x,k,v) -tanh(x+k*v);
and solve it numerically.
There is a recent relevant example of that in this thread, so I will not repeat it here.
To illustrate —
figure
fplot(@(x)-sign(x), [-5 5])
hold on
fplot(@(x)-tanh(5*x), [-5 5])
hold off
ylim([-1.5 1.5])
legend('-sign(x)','-tanh(5*x)', 'Location','best')
grid

.
Juan Hurtado
2021 年 5 月 3 日
Perfect! Well I that solves all my doubts. I reall appreciate all your help.
Thank you so much!
Star Strider
2021 年 5 月 3 日
As always, my pleasure!
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Equation Solving についてさらに検索
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の MathWorks のサイトは、お客様の地域からのアクセスが最適化されていません。
南北アメリカ
- América Latina (Español)
- Canada (English)
- United States (English)
ヨーロッパ
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
