Parse error: usage might be invalid MATLAB syntax

8 次查看(过去 30 天)
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 个评论
Walter Roberson
Walter Roberson 2018-10-19
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...

请先登录,再进行评论。

回答(1 个)

madhan ravi
madhan ravi 2018-10-19
编辑:madhan ravi 2018-10-19
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

类别

Help CenterFile Exchange 中查找有关 Argument Definitions 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by