How to use quiver with equations in function?
3 views (last 30 days)
Show older comments
I have defined several functions with equations like
function dZ = ODE(~,z)
x = z(1);
y = z(2);
dZ = [x+y;...
4*x-2*y];
end
These functions are used by other functions for solving etc. Now I want to do plotting with quiver but I don't figure out how to pass a function with equations as a parameter to quiver. It should look like this:
odehandle = str2func('ODE');
plot_vectorfield(odehandle);
function plot_vectorfield(odehandle)
[x,y]=meshgrid(-10:1:10,-10:1:10);
[dx,dy]=odehandle; %does not work!
quiver(x,y,dx,dy);
end
So the problem is obviously how to pass the u,v parameter to quiver. I would also like to know how to handle function defined equation systems with 3 equations with quiver. Thanks a lot!
0 Comments
Accepted Answer
Walter Roberson
on 23 Sep 2017
Edited: Walter Roberson
on 23 Sep 2017
Replace
[dx,dy]=odehandle; %does not work!
with
temp = arrayfun(@(X, Y) odehandle([],[X,Y]), x, y, 'uniform', 0);
dx = cellfun(@(C) C(1), temp);
dy = cellfun(@(C) C(2), temp);
This is for the special case that the ODE is a function of two variables and the first derivatives apear as the first two elements of the output vector.
The 3D equivalent would be something like,
function plot_vectorfield(odehandle)
[x, y, z]=meshgrid(-10:1:10, -10:1:10, -10:1:10);
temp = arrayfun(@(X, Y, Z) odehandle([],[X,Y,Z]), x, y, z, 'uniform', 0);
dx = cellfun(@(C) C(1), temp);
dy = cellfun(@(C) C(2), temp);
dz = cellfun(@(C) C(3), temp);
quiver3(x, y, z, dx, dy, dz);
end
More Answers (0)
See Also
Categories
Find more on Vector Fields in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!