When I run this function it gives me the following error:

function [S]=Signfunction(f,a,b)
v=a:0.5:b;
n=length(v);
S=zeros(1,n);
for i=1:n
if f(i)<0
S(1,i)=-1;
else
S(1,i)=1;
end
end
Signfunction((v.^2)+(2.*v)-9,2,4)
Undefined function or variable 'v'.

Answers (2)

You have not defined ‘v’ in your script before you called your ‘Signfunction’ function.
In context, you want to evaluate the function ((v.^2)+(2.*v)-9 inside the SignFunction function. To do that, define it as an anonymous function. See the first block of code that defines the anonymous function stored in the variable sqr for an example.
If this is for a homework assignment you could check your results against the sign function included with MATLAB, though it returns 0 where the input vector is 0 instead of returning 1 as your function does.

Tags

Asked:

on 23 Oct 2016

Answered:

on 23 Oct 2016

Community Treasure Hunt

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

Start Hunting!