How does evaluation of a function for each x and y coordinate in a matrix work?

I am trying to solve a problem for one of my classes. I tried a few different codes based on ones the professor provided and that I found online, but I can't get anything to work. I found this code on here and it works to give a 3d plot of the function f(x,y)=x^2*y. However, if I change the "*" to a "-" or "+" and change nothing else, the code no longer works. Why is that? (The problem I am trying to solve is f(x,y)=(y-C)(3x^2-y^2) where C is a constant found earlier in the code, I'm not having an issue with that part)
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]);

6 Commenti

Okay, I found out it works with "+" or "-" if you remove the period, but the answers it gives seem strange. It gives "fxy = xm^2 + ym;" as a flat plane when the x^2 term should make it curve and gives "fxy = xm^2 + ym^2;" as as surfaces that is rough, pot-holed, erratic and seemingly random like the surface of the moon close up.
  • "if I change the "*" to a "-" or "+" and change nothing else, the code no longer works" I change .* to + and it works just fine on R2016a. See Array vs. Matrix Operations
  • "the code no longer works" more details please!
I would love to know what math it is doing behind the scenes, and what the significance of the period or dot is in the original code. All I want it to do is to evaluate a function of x and y for a set of x and y terms and give me a plot of the result.
Per Isakson, if I run the original code with the "*" it gives me a 3d map of a curve that looks like a good evaluation of that function. If the "*" replaced by a "-" or "+" it says "Error: File: test2.m Line: 4 Column: 13 Unexpected MATLAB operator."
  • "love to know what math it is doing behind the scenes" Did you use the link I provided?
  • "Unexpected MATLAB operator" .+ is not a Matlab operator, but .* is. See MATLAB Operators and Special Characters.
Per Isakson, yes, I'm reading it now, thank you very much.

Accedi per commentare.

Risposte (0)

Richiesto:

il 31 Mar 2018

Modificato:

il 31 Mar 2018

Community Treasure Hunt

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

Start Hunting!

Translated by