how to draw a slope field in matlab

心不动则不痛 提交于 2020-01-02 05:20:10

问题


I was looking for a way to draw slope fields in Matlab.

Here is what I am looking for:

I have an equation

dy/dx = f(x,y)

or

dx/dt = f(x,y)
dy/dt = g(x,y)

and I want to draw it in a nice way

Because the only answer about it here was not answering my question, it took me some time to find how to do this.

Also because this is not something I am doing all the time in matlab (most probably till the next time I will need it, I will forget it) I am creating a memo for me how to do this.

If you will find it useful, feel free to upvote


回答1:


so here is the equation:

dx/dt = x^2-3xy+y
dy/dt = -5x+sin(yx)

That is the code, which will help to do the job:

[x,y] = meshgrid(-2:0.2:2);
dx = x.^2-3*x.*y+y;
dy = -5*x+sin(x.*y);
r = ( dx.^2 + dy.^2 ).^0.5;
px = dx./r;
py = dy./r;
quiver(x,y,px,py);

It is also possible to use the package dfield. You can read it here. But I have not tested it for myself



来源:https://stackoverflow.com/questions/15747718/how-to-draw-a-slope-field-in-matlab

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