Plotting 3D Graph with MATLAB Function

In summary, the conversation is about passing a function with parameters, including an equation, to MATLAB and generating a 3D graph. The person is having trouble with the function and getting an error message. The expert suggests looking up "help function pointer" and making two changes to the code.
  • #1
stihl29
25
0
Hi, i need to pass a function in MATLAB a few parameters, one of the parameters is an equation such as :
sin(x)-cos(y)
and have it make the 3d graph, i have no problem setting up the meshgrid etc and getting it to work running strait from the m-file

function graph = graph(f)
n=20;
x = (-n:1:n);
y = (-n:1:n);
[x,y] = meshgrid(x,y);

z=f;

surf(x,y,z);

end

example of running the function

graph(sin(x)-cos(y))

right now when i run it i get.

? Error using ==> surf at 78
Z must be a matrix, not a scalar or vector.

Error in ==> graph at 7
surf(x,y,z);


Any advice would be great.
 
Physics news on Phys.org
  • #2
Look up "help function pointer" and follow their examples.
 
  • #3
Two things,

1) Change the line z = f to z = f(x,y)

2) When you call the function, use graph(@(x,y) cos(x) - sin(y))
 

Related to Plotting 3D Graph with MATLAB Function

1. How do I plot a 3D graph with a MATLAB function?

To plot a 3D graph with a MATLAB function, you can use the "plot3" function. This function takes in three input arguments: x, y, and z coordinates, and plots them in a 3D space. You can also add a fourth argument to specify the color and style of the plot.

2. Can I customize the appearance of my 3D graph?

Yes, you can customize the appearance of your 3D graph by using additional functions such as "xlabel", "ylabel", and "zlabel" to add labels to the axes, and "title" to add a title to your graph. You can also change the color, style, and size of the points or lines in your graph.

3. How do I add a legend to my 3D graph?

To add a legend to your 3D graph, you can use the "legend" function. This function takes in a cell array of strings as an input, where each string represents a label for a specific data set or plot. You can also specify the location of the legend using the "Location" argument.

4. Can I save my 3D graph as an image?

Yes, you can save your 3D graph as an image by using the "saveas" function. This function takes in the figure handle and the desired file format as input arguments. You can also specify the resolution and other properties of the image using additional arguments.

5. Is it possible to plot multiple 3D graphs in one figure?

Yes, it is possible to plot multiple 3D graphs in one figure by using the "hold on" and "hold off" commands. The "hold on" command allows you to plot multiple graphs on the same figure, while the "hold off" command ends the plotting session and resets the figure for the next plot. You can also use the "subplot" function to create subplots within a figure.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
527
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
236
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
571
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
598
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
Back
Top