如何在Matlab中绘制非线性微分方程 [英] How do you plot nonlinear differential equations in matlab
问题描述
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.
现在我被卡住了:(
如何在不求解方程的情况下绘制该系统?
how can you plot this system without solving the equations?
推荐答案
首先定义要求解的微分方程.它必须是一个带有两个参数的函数-当前时间t
和当前位置x
,并返回列向量.代替x和y,我们将使用x(1)
和x(2)
.
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];
现在使用ode45
数值求解方程:
Now solve the equation numerically using ode45
:
ode45(f, tspan, xinit)
这将导致该图:
如果要及时获取解决方案的值,则只需询问一些输出参数即可:
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);
您可以通过这样做
plot(y(:,1), y(:,2)), xlabel('x'), ylabel('y'), grid
这将导致以下情节
这篇关于如何在Matlab中绘制非线性微分方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!