Parse error: usage might be invalid MATLAB syntax

53 ビュー (過去 30 日間)
Abraham
Abraham 2018 年 10 月 19 日
コメント済み: Walter Roberson 2018 年 10 月 19 日
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
  1 件のコメント
madhan ravi
madhan ravi 2018 年 10 月 19 日
編集済み: madhan ravi 2018 年 10 月 19 日
I leave the rest to you to debug

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeArgument Definitions についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by