MATLAB function

  • MATLAB
  • Thread starter stihl29
  • Start date
  • #1
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.
 

Answers and Replies

  • #2
1,679
3
Look up "help function pointer" and follow their examples.
 
  • #3
166
0
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 Threads on MATLAB function

  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
2
Views
6K
  • Last Post
Replies
2
Views
3K
  • Last Post
Replies
1
Views
1K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
3
Views
11K
  • Last Post
Replies
2
Views
7K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
0
Views
12K
  • Last Post
Replies
1
Views
24K
Top