problem with getting for loop to work
조회 수: 2 (최근 30일)
이전 댓글 표시
function int=calculateintegral(from,to,subinterval)
inttemp=0;
if rem(subinterval,2)>0
int='subinterval must be devidable on two'
else
for x=from+subinterval:subinterval*2:to-subinterval
y1=4*exp(-(x)^2)
y2=2*exp(-(x+((to-from)/subinterval))^2)
inttemp=inttemp+y1+y2
end
end
int=inttemp
when i ran it in matlab it does not seem to loop through the for-loop or the if-condition:
>> calculateintegral(0,1,1000)
int =
0
ans =
0
>> calculateintegral(0,1,1001)
int =
0
ans =
0
댓글 수: 0
채택된 답변
Fangjun Jiang
2011년 11월 12일
Use your input argument, your for-loop looks like this:
for x=1000:2000:-999
No loop is executed at all.
I don't think your subinterval is defined correctly. Running calculateintegral(0,100,2) seems to give you some results.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!