Parse error: usage might be invalid MATLAB syntax

51 views (last 30 days)
So, I'm to write a function called trapint.m that takes data sample locations and function samples at those locations as input and returns an approximation of the integral over the sample range based on the trapezoidal rule. This is the detail of the instruction
This is what I did:
function I=trapint(x,fx)
N= max(size(fx));
N= max(size(x));
If n==N;
If isvector(x)==isvector(fx)==1;
If Isnumeric(x) == isnumeric (fx)==1;
h=(fx(1)-fx(N))/N;
i=2; y=0;
while i<=n-1 %%for summation of middle no between a and b
y = y+(fx(i));
i=i+1;
end
I=h/2*(fx(1)+2*y+fx(N));
ans=double(ans);
end
else
sprintf('error = x and fx are not of same length')
end
else
sprintf ('error =x and fx are not vectors')
end
else
Sprintf ('error= either data contained other than numerical value')
end
I got parse errors and invalid characters in lines 27,31,33,35,39 and 41. Thanks in advance
  1 Comment
Walter Roberson
Walter Roberson on 19 Oct 2018
If isvector(x)==isvector(fx)==1;
tries to invoke a function named If with a single argument, like
If('isvector(x)==isvector(fx)==1');
You probably do not have a function named If. You probably do not have a function named Sprintf either.
MATLAB is case sensitive.
Your code has several Zero-Width Space characters https://www.fileformat.info/info/unicode/char/200B/index.htm . You can't see them, of course...

Sign in to comment.

Answers (1)

madhan ravi
madhan ravi on 19 Oct 2018
Edited: madhan ravi on 19 Oct 2018
fx=1:10 %an example of the vectors
x=10:20
I = trapint(x,fx)
function I=trapint(x,fx)
N= max(size(fx));
N= max(size(x));
if nargin(trapint)<2
error('inputs must be two ')
if size(fx)~=size(x)
error('not same size')
if isvector(x)~=1 & isvector(fx)~=1
error('not vectors')
h=(fx(1)-fx(N))/N;
i=2; y=0;
while i<=n-1 %%for summation of middle no between a and b
y = y+(fx(i));
i=i+1;
end
I=h/2*(fx(1)+2*y+fx(N));
I=double(I); % whats ans here???
end
end
end
end

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!