I keep getting this error...

The error is: Error using mesh (line 70) Z must be a matrix, not a scalar or vector.
Here is my code, and I do not know how to correct it so this error does not appear. I am trying to plot a mesh plot of the function I have denoted as g with the x and y intervals that are listed below.
%defining an x-interval
x = [0:0.2:2];
%defining an y-interval
y = [0:0.2:2];
%writing the equation
g = ((exp(1*-y)).*(sin(x)+cos(x)));
%attempting to solve the function
z= polyval(g, x, y)
%
%saving values for g
save('datafile1.mat', 'z')
%plotting the equation as a mesh
[i,j]= meshgrid(x,y);
mesh (i, j, z)
grid on;
hold on;
Can someone please help? Please and Thank you!

 Accepted Answer

Your polyval call is a bit mystifying. You already have your ‘g’ function defined, so just vectorise it an then plot it.
The Code —
%defining an x-interval
x = [0:0.2:2];
%defining an y-interval
y = [0:0.2:2];
%writing the equation
g = @(x,y) ((exp(-y*1)).*(sin(x)+cos(x)));
[i,j]= meshgrid(x,y);
z = g(i,j);
figure(1)
mesh(i, j, z)
grid on

2 Comments

Thank you!
As always, my pleasure!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!