How do I fix an infinite loop?
이전 댓글 표시
I tried using Simpson's 1/3rd rule for multiple segments. Can someone please tell me why this code isn't working?
%Specific Heat f(T) = a+bT+CT.^2
function [Cp]=f(T,a,b,c)
Cp=a+(b*T)+(c*(T^2));
end
a=input('a = ');
b=input('b = ');
c=input('c = ');
T1=input('Initial Temperature T1 = ');
T2=input('Final Temperature T2 = ');
n=input('Number of divisions ');
h=abs(T2-T1)/n;
sum2=0;
sum3=0;
for i=(T1+h):h:(T2-h)
sum1=f(T1,a,b,c)+f(T2,a,b,c);
while even(i)==1
sum2=sum2+f(i,a,b,c);
end
while even(i)==0
sum3=sum3+f(i,a,b,c);
end
sum=sum1+(2*(sum2))+(4*(sum3));
Value=(h/3)*sum;
disp(Value);
end
댓글 수: 1
Image Analyst
2017년 11월 26일
편집: Matt J
2017년 11월 26일
채택된 답변
추가 답변 (1개)
Roger Stafford
2017년 11월 26일
You are using the 'even' function on temperatures instead of index values. You should probably have something like:
h = (T2-T1)/n;
T = linspace(T1,T2,n+1); % n must be even
Value = h/3*(f(T1,a,b,c)+4*sum(f(T(2:2:n),a,b,c)) ...
+2*sum(f(T(3:2:n-1),a,b,c))+f(T2,a,b,c));
카테고리
도움말 센터 및 File Exchange에서 Simulink에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!