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

3 views (last 30 days)
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?

Accepted Answer

Guillaume
Guillaume on 30 May 2019
Edited: Guillaume on 30 May 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;

More Answers (0)

Categories

Find more on Creating and Concatenating Matrices in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!