Parse error: usage might be invalid MATLAB syntax

45 visualizaciones (últimos 30 días)
Abraham
Abraham el 19 de Oct. de 2018
Comentada: Walter Roberson el 19 de Oct. de 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 comentario
Walter Roberson
Walter Roberson el 19 de Oct. de 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...

Iniciar sesión para comentar.

Respuestas (1)

madhan ravi
madhan ravi el 19 de Oct. de 2018
Editada: madhan ravi el 19 de Oct. de 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
  1 comentario
madhan ravi
madhan ravi el 19 de Oct. de 2018
Editada: madhan ravi el 19 de Oct. de 2018
I leave the rest to you to debug

Iniciar sesión para comentar.

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by