Ältere Kommentare anzeigen
How do I plot z=.5u1^2+u1u2+u2^2+u2 in 3D and also in 2D on the u1 vs u2 axis for a couple values of z? I'm new to matlab and am having trouble setting up variables. Any help would be much appreciated.
Tyler
Antworten (1)
Andrew Newell
am 9 Feb. 2011
First you need to decide what range of values you want to plot:
u1 = -1:.01:1; u2 = -1:.01:1;
Then create matrices:
[u1,u2] = meshgrid(u1,u2);
Then calculate z. Note the dots, meaning element-by-element multiplication and squares (zo z(1,1) = .5*u1(1,1)^2+u1(1,1)*u2(1,1) + ...) z=.5*u1.^2+u1.*u2+u2.^2+u2;
surf(u1,u2,z);
xlabel('u1'); ylabel('u2'); zlabel('z'); label the axes
To find u1 and u2 for a given z is generally harder. For this problem, if you have the Symbolic Toolbox, you can do this:
syms u1 u2 z
equationsForU2 = solve(.5*u1.^2+u1.*u2+u2.^2+u2-z,u2)
This gives you two expressions for u1 in terms of u2 and z. Now you could use ezplot to plot the solution for a given value of z (e.g., z=0):
figure
ezplot(subs(g(1),z,0))
hold on
ezplot(subs(g(2),z,0))
I'll leave it to you to figure out how to adjust the axes so the whole curve fits on the plot.
Kategorien
Mehr zu 2-D Function Plots finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!