필터 지우기
필터 지우기

Why do my loops only run once

조회 수: 1 (최근 30일)
Raquel Terrer van Gool
Raquel Terrer van Gool 2020년 4월 14일
댓글: Raquel Terrer van Gool 2020년 4월 14일
function I=simpson13(f,a,b,n)
h=(b-a)/n; % length of the interval
total=0;
x=a;
fa=f;
x=b;
fb=f;
for i=1:2:n-1;
x=i*h;
fn=f+total;
total=fn;
end
for j=2:2:n-2;
x=j*h;
fm=f+total;
total=fm;
end
I=(h/3)*(fa+fb+4*fn+2*fm);
end
  댓글 수: 1
Raquel Terrer van Gool
Raquel Terrer van Gool 2020년 4월 14일
편집: Geoff Hayes 2020년 4월 14일
I have n=6:
x=0;
n=6; % n is the number of intervals and must be > 0 and even
a=0; b=pi;% a is the lower bound and b is the upper bound
f=5+13*sin(x); % Function
I=simpson13(f,a,b,n)

댓글을 달려면 로그인하십시오.

채택된 답변

Geoff Hayes
Geoff Hayes 2020년 4월 14일
Raquel - the MATLAB debugger is helpful in cases like this. If you put a breakpoint in your simpson13 you would notice a problem with the input parameter f. Look closely at how it is being assigned
x=0;
n=6; % n is the number of intervals and must be > 0 and even
a=0; b=pi;% a is the lower bound and b is the upper bound
f=5+13*sin(x); % Function
f is not a function in this case but is a scalar value (5) since x is zero. If you want f to be an anonymous function then you need to assign it as
f = @(x)5+13*sin(x);
where nthe @ operator creates the handle, and the parentheses () immediately after the @ operator include the function input arguments. You will need to change how this function is used in your code as well. For example,
fa=f(a);
fb=f(b);
etc.
  댓글 수: 1
Raquel Terrer van Gool
Raquel Terrer van Gool 2020년 4월 14일
I solved the problem with your help. Thank you very much!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Mathematics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by