Parse error: usage might be invalid MATLAB syntax

50 Ansichten (letzte 30 Tage)
Abraham
Abraham am 19 Okt. 2018
Kommentiert: Walter Roberson am 19 Okt. 2018
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 Kommentar
Walter Roberson
Walter Roberson am 19 Okt. 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...

Melden Sie sich an, um zu kommentieren.

Antworten (1)

madhan ravi
madhan ravi am 19 Okt. 2018
Bearbeitet: madhan ravi am 19 Okt. 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

Kategorien

Mehr zu Argument Definitions finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by