0

keep getting an error line 43, assignment to simpson not supported. variable has the same name as a local function.

Did i code this correctly if anyone on here is familiar with matlab/simpsons rule? line 43 is mu function line..


a=input("Enter value of a:");

b=input("Enter value of b:");

n=input("Enter value of n:");

x=linspace(a,b,n+1);

y=exp(-x.^2);


function [simpson] = simpson(x,y)

sum=0;

if mod(length(x)-1,2)==1

disp('Cannot run code.')

else

for i=2:length(x)-1

if mod(i,2)==0

sum=sum+2*y(i);

else

sum=sum+4*y(i);

end

end

end

sum=sum+y(1)+y(x(end));

simpson=sum;

end
  • 3
    You identified the problem yourself, the variable ahs the same name as the local function - change it. Also stop using the in-built function name `sum` as a variable name to avoid the same issue. – Wolfie Nov 16 '21 at 08:09
  • +1 for Wolfie. On top of the issues mentioned by them, `i` has a built in value in `MATLAB`, so it is bad practice to use it as a variable name as you have here. For loop indices I like to use `k`, `ii` or `i1`, `i2`,..., etc. – Dan Pollard Nov 16 '21 at 09:44

0 Answers0