Undefined function or variable 'npoints'.
Show older comments
It runs on matlab but the reason why it keep saying an error message is that i get wrong answer out of this function.
below is error message that i get this time
Undefined function or variable 'npoints'. Error in tutorial2 (line 7) midx=zeros(npoints,1); Error in Test1
i'm getting 75.0082 with this code on matlab
But the error percentage i'm supposed to have is around 0.0655
Could you see this again? thanks
function err = tutorial2(nPoints)
%% Midpoint rule for integral 4/(1 + x^2) from x = 0 to x = 1
% inputs: - nPoints number of points in domain
% outputs: - err percentage error (absolute value)
x=linspace(0,1,nPoints);
dx=x(2)-x(1);
midx=zeros(npoints,1);
numSol=0;
for i = 0:nPoints-1
midx(i)=(x(i)+x(i+1))/2;
fvalue=4/(1 + midx(i)^2);
numSol=numSol+fvalue;
end
numSol=numSol*dx;
analyticalSol=pi/4;
err=abs((analyticalSol-numSol)*100/numSol);
end
Answers (1)
Walter Roberson
on 9 Mar 2019
0 votes
MATLAB is case sensitive. You used npoints on line 7, but you do not define that variable. You define nPoints with a capital P, which is a different variable.
Categories
Find more on MATLAB Coder 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!