How to plot "f(x,y) = x^2 * y" function in matlab?

12 Ansichten (letzte 30 Tage)
Minh Phuoc
Minh Phuoc am 26 Okt. 2013
Kommentiert: Minh Phuoc am 27 Okt. 2013
I am a beginner, so I am familiar with matlab. I am trying to plot a function: f(x, y) = x^2 * y, but it looks like something wrong. I am also trying another way but it looks different with a result I got from Google. Is there anybody have experience this problem, help me please?
The method I am trying is: x = rcost, y = rsint -> f(r, t) = r^2 * cos^2(t) * r * sin(t)
ra = linspace(-2.0, 2.0);
theta = linspace(-0.5*pi, 0.5*pi);
[TH, RA] = meshgrid(theta, ra);
F = RA.^3 * cos(TH) * cos(TH) * sin(TH);
surf(TH,RA,F);
shading interp;
  3 Kommentare
Image Analyst
Image Analyst am 27 Okt. 2013
Bearbeitet: Image Analyst am 27 Okt. 2013
linspace takes 3 arguments, not 2. Anyway, see my answer below, which follows your original method, not the Google trig method.
the cyclist
the cyclist am 27 Okt. 2013
linspace() can accept two arguments. It will default to 100 generated points.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Image Analyst
Image Analyst am 27 Okt. 2013
Try this:
x = linspace(-2.0, 2.0, 50);
y = linspace(-2.0, 2.0, 50);
[xm, ym] = meshgrid(x, y);
fxy = xm.^2 .* ym;
surf(fxy);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
It gives you almost exactly what you want. You just have to rotate the viewpoint of the axis. I trust you can do this.
  2 Kommentare
Minh Phuoc
Minh Phuoc am 27 Okt. 2013
thank you. I got it.
Minh Phuoc
Minh Phuoc am 27 Okt. 2013
I want to visualize the Lagrange Multiplier for f(x, y) = x^2 * y, with x, y lie on the circle around the origin with radius is square root of 3, g(x, y) = x^2 + y^2 - 3. So I can plot the f function but it is too large and the circle is too small. Do you know how to scale or resize it like this image?
My result is:
The Matlab source code is:
x = linspace(-2.0, 2.0, 50);
y = linspace(-2.0, 2.0, 50);
[xm, ym] = meshgrid(x, y);
fxy = xm.^2 .* ym;
surf(fxy);
shading interp;
hold all;
theta = linspace(0, 2*pi);
[xmc, ymc] = pol2cart(theta, sqrt(3));
plot3(xmc, ymc, xmc+ymc);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

the cyclist
the cyclist am 26 Okt. 2013
I expect you are doing a matrix multiplication where you had actually wanted an element-by-element multiplication. Try this line instead:
F = RA.^3 .* cos(TH) .* cos(TH) .* sin(TH);
Notice how I modified your multiplications.
  1 Kommentar
Minh Phuoc
Minh Phuoc am 26 Okt. 2013
I tried it, but it still does not work. This is the output.
The expected output is

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Animation finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by