How to use quiver with equations in function?

5 vues (au cours des 30 derniers jours)
Sam Finch
Sam Finch le 23 Sep 2017
Modifié(e) : Sam Finch le 24 Sep 2017
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!

Réponse acceptée

Walter Roberson
Walter Roberson le 23 Sep 2017
Modifié(e) : Walter Roberson le 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
  1 commentaire
Sam Finch
Sam Finch le 23 Sep 2017
Modifié(e) : Sam Finch le 24 Sep 2017
Thanks a lot, works like a charm.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Vector Fields dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by