Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to draw a slope field in matlab

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

like image 746
Salvador Dali Avatar asked Dec 27 '22 06:12

Salvador Dali


1 Answers

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

like image 139
Salvador Dali Avatar answered Jan 05 '23 00:01

Salvador Dali