Simpson's 1/3 rules

3 Ansichten (letzte 30 Tage)
Nasser Yari
Nasser Yari am 31 Mär. 2016
Bearbeitet: Walter Roberson am 1 Apr. 2016
Hi,
I am not sure why this does not work.Should I replace the i with j?I appreciate any help.
function I=mysimp(f,a,b,n)
h=(b-a)/n;
sum4=0;
sum2=0;
for i=2:2:n
x(i)=a+i*h;
y(i)=f(x(i))
sum4=sum4+4*feval(f,x(i));
end
if i=3:2:n-1;
x(i)=a+i*h;
y(i)=f(x(i))
sum2=sum2+2*feval(f,x(i));
end
sum=sum2+sum4+feval(f,a)+feval(f,b);
I=sum*h/3;
plot(x,y)
this is what I get
Error: File: mysimp.m Line: 10 Column: 9
The expression to the left of the equals sign is not a valid target for an assignment.

Akzeptierte Antwort

John D'Errico
John D'Errico am 31 Mär. 2016
A TERRIBLE idea to use the name sum as a variable. You WILL get int trouble later on if you continue to use incredibly useful function names as variable names too. And then your anquished question will be WHAT IS WRONG WITH MY CODE????
Regardless, the following line is not valid syntax in MATLAB:
if i=3:2:n-1;
  1 Kommentar
Nasser Yari
Nasser Yari am 1 Apr. 2016
Thanks for the advice. I found out where I made a typo mistake if should be for.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Numerical Integration and Differential Equations 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