Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.


Curious
Posts:
2,357
Registered:
12/6/04


Re: Use of trapz in a while loop
Posted:
Mar 6, 2014 2:21 PM


"Jonas" wrote in message <lfadvc$h3l$1@newscl01ah.mathworks.com>... > Hi, > > I am looking to run someting until the area of two graphs are the same. One graph is given by a function, the other by values of a tabel. > > I am hoping to find the at what time the two areas are alike, and the temperature is above 300 celsius. > > Can someone please help me, i only get that the time must be 0 (zero) but cant get why. (answer is supposed to be 0.75 hours) > > My code: > > N=20; > t=linspace(0,1,N); %Time , hour(s) > > %Standard curve > > hold on > grid on > t_0=20; %celsius , starting temperature > > T=t_0+345*log10(0.133.*(t*3600)+1); > plot(t,T,'b'); > > %Comp. temptime Petterson (1976) > > t_h=[0.05 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 1.1 1.2 1.3 1.4... > 1.5 1.6 1.7 1.8 1.9]; > > temp=[602 854 862 888 860 727 589 525 456 381 329 304 279 254 229 204 180 ... > 154 127 99]; > > hold on > plot(t_h,temp,'r') > legend('Standardcurve','Petterson timetemp. curve') > xlabel('Time [h]') > ylabel('Temperatur [C]') > axis([0 1 0 1000]) > > %Equal area > > k=1; > > while (trapz(t_h(1:k),temp(1:k)))>(trapz(t(1:k),T(1:k)))... > && temp(1:k) > 300 > > k=k+1; > end > > disp('Time') > t(k)
I think your problem is with the
&& temp(1:k) > 300
part of the while loop. You are comparing a vector to a scalar. I believe MATLAB interprets this as if ANY (or is it ALL?) elements of the vector are greater than 300, then ....
Perhaps you mean
&& temp(k) > 300
In any case, what should happen if k > length(temp)? You need to check for this case.
Also, just to be sure, you might consider changing
while (trapz(t_h(1:k),temp(1:k)))>(trapz(t(1:k),T(1:k)))...
to
while (trapz(t_h(1:k),temp(1:k))>trapz(t(1:k),T(1:k)))...
if that is what you mean.
I don't have MATLAB installed on this computer, so that is my best guess.



