How do you plot nonlinear differential equations in matlab

╄→гoц情女王★ 提交于 2019-12-19 04:37:49

问题


Dx=y
Dy=-k*y-x^3+9.8*cos(t)
inits=('x(0)=0,y(0)=0')

these are the differential equations that I wanted to plot.

first, I tried to solve the differential equation and then plot the graph.

Dsolve('Dx=y','Dy=-k*y-x^3+9.8*cos(t)', inits)

like this, however, there was no explicit solution for this system.

now i am stuck :(

how can you plot this system without solving the equations?


回答1:


First define the differential equation you want to solve. It needs to be a function that takes two arguments - the current time t and the current position x, and return a column vector. Instead of x and y, we'll use x(1) and x(2).

k = 1;
f = @(t,x) [x(2); -k * x(2) - x(1)^3 + 9.8 * cos(t)];

Define the timespan you want to solve over, and the initial condition:

tspan = [0, 10];
xinit = [0, 0];

Now solve the equation numerically using ode45:

ode45(f, tspan, xinit)

which results in this plot:

If you want to get the values of the solution at points in time, then just ask for some output arguments:

[t, y] = ode45(f, tspan, xinit);

You can plot the phase portrait x against y by doing

plot(y(:,1), y(:,2)), xlabel('x'), ylabel('y'), grid

which results in the following plot



来源:https://stackoverflow.com/questions/16023579/how-do-you-plot-nonlinear-differential-equations-in-matlab

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!