Shamendra
Shamendra

Reputation: 336

Matlab Error- fplot();

>> fplot(fh,[-2 4])
??? Undefined function or variable "e".

Error in ==> myfun at 3
Y(:,2) = e(:).^x;
Error in ==> fplot at 102
x = xmin; y = feval(fun,x,args{4:end});

I tried to plot two function using this m file.

function Y = myfun(x)
Y(:,1) = 3*x;
Y(:,2) = e(:).^x;

Upvotes: 0

Views: 2871

Answers (1)

Jonas
Jonas

Reputation: 74940

As Donnie mentioned in their comment, the variable e is undefined in your m-file.

If you have defined e elsewhere, you have to pass it to myfun so that the function knows its value. Since fplot does not accept plotting functions with more than one input value, you need to pass it an anonymous function.

First, you need to change the definition of myfun to include e as input:

function Y = myfun(x,e)
Y(:,1) = 3*x;
Y(:,2) = e(:).^x;

Then, you create your function handle fh like this (fh still only takes one input, Matlab uses the value of e as it was defined in the workspace at the time you create the function handle):

fh = @(x)(myfun(x,e))

Finally, you can call fplot like you used to

fplot(fh,[-2 4])

Upvotes: 1

Related Questions