Plotting a surface in MATLAB
1 visualizzazione (ultimi 30 giorni)
Mostra commenti meno recenti
Anthont Goddard
il 3 Ott 2015
Commentato: Star Strider
il 3 Ott 2015
I would like to plot
f(x,y) = x^2*y/(x^2 + y^2)
How do I do this in matlab?
2 Commenti
Risposta accettata
Star Strider
il 3 Ott 2015
You need to use the meshgrid (or equivalently, ndgrid) function:
x = linspace(-1, 1);
y = linspace(-2, 2);
[X,Y] = meshgrid(x,y);
f = @(x,y) x.^2.*y./(x.^2 + y.^2);
figure(1)
surf(X,Y,f(X,Y))
grid on
I created a function for ‘f’ here, but you could as easily use it simply as:
f = X.^2.*Y./(X.^2 + Y.^2);
and then plot as:
plot(X,Y,f)
I ‘vectorised’ your code to do element-wise operations. See the documentation on Array vs. Matrix Operations for a full discussion.
4 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Line Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!