6 hump camel function - What is wrong with the code?

4 Ansichten (letzte 30 Tage)
Hello,
I am trying to plot the 6 hump camel back function using a simple code as shown below:
[x,y]=meshgrid(-2:0.02:2,-1:0.01:1);
z=((4-(2.1*(x.^2))+((x.^4)/3))*(x.^2))+(x.*y.*1)+(4*(-1+(y.^2))*(y.^2));
mesh(x,y,z)
A plot is made but it does not match the actual function at all. The term (x.*y.*1) was written so since an error was observed when I dropped the *1 (Invalid expression. When calling a function or indexing a variable, use parentheses. Otherwise, check for mismatched delimiters.). How do I correct this?

Akzeptierte Antwort

Guillaume
Guillaume am 30 Mai 2019
Bearbeitet: Guillaume am 30 Mai 2019
The overuse of unnecessary brackets and the lack of any spacing make your expression very hard to read.
Multiplying by 1 will never change the result and will never make any difference to any error.
Your expression, without all the unnecessary brackets and with some spacing:
z = (4 - 2.1*x.^2 + x.^4/3)*x.^2 + x.*y + 4*(-1 + y.^2)*y.^2;
In my opinion much easier to read, and you can immediately see the two errors. You're doing matrix multiplication with x.^2 and y.^2 instead of element-wise multiplication. Changing the two * into .* is probably what you want:
z = (4 - 2.1*x.^2 + x.^4/3).*x.^2 + x.*y + 4*(-1 + y.^2).*y.^2;

Weitere Antworten (0)

Kategorien

Mehr zu Creating and Concatenating Matrices finden Sie in Help Center und File Exchange

Produkte


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by