Writing differential equation for plot

Can anyone help me how to write differential below equation for plot
d2x/dt2 = -sign(x + dx/dt)
I tried in below way and it is failing
ode = diff(x,t,2) == -sign(x + diff(x,t));

 採用された回答

Ameer Hamza
Ameer Hamza 2020 年 12 月 5 日

0 投票

Try this
syms x(t)
d1x = diff(x);
d2x = diff(x,2);
ode = d2x == -sign(x + d1x);
cond = [x(0)==1 d1x(0)==0]
sol = dsolve(ode, cond)
This requires the Symbolic Math toolbox.

5 件のコメント

vaibhav gupta
vaibhav gupta 2020 年 12 月 5 日
I am getting error on running this way
Array indices must be positive integers or logical values.
Error in sym/subsref (line 898)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in Test2 (line 10)
plot(sol(t));
Ameer Hamza
Ameer Hamza 2020 年 12 月 5 日
Use following line to create plot
fplot(sol)
vaibhav gupta
vaibhav gupta 2020 年 12 月 5 日
Thank you! What is difference between plot vs fplot? Why plot was failing?
Ameer Hamza
Ameer Hamza 2020 年 12 月 6 日
fplot() is used when you want to plot a function handle or s symbolic function. On the other hand, plot() requires x, y data points. To use the plot() function here, you will need to define an x vector and calculate the y-values yourself. fplot() take cares of all these calculations
syms x(t)
d1x = diff(x);
d2x = diff(x,2);
ode = d2x == -sign(x + d1x);
cond = [x(0)==1 d1x(0)==0];
sol = dsolve(ode, cond);
xv = linspace(-5, 5);
yv = subs(sol, t, xv);
plot(xv, yv);
vaibhav gupta
vaibhav gupta 2020 年 12 月 6 日
Thank you for explaining difference. If i want to use ode45 function, how can i do that?

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by