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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With