I have a looping question about a problem that I am doing. Could someone please help?

1 次查看(过去 30 天)
I've been trying to figure out a way to loop through an iterative process using matlab, but I can't figure out the correct way to run it. I have a start condition gamma (y) and a start temperature (T2) that is a function of gamma: T2 = #*T1*y. T1 is a constant initial temperature. To iterate and find a correct y, T1 and T2 have to be averaged and put into an equation that will develop a second value for y. This second value for y will be put back into the first equation: T2 = #*T1*y(2) to find a better value of T2. The final value of T2 will be when T2 changes very little: tolerance = 0.1%. I have an outline of this process below:
y(1) = 1.4
T2 = #*T1*y(1)
Tavg = (T2 + T1)/2
y(2) = #*Tavg
T2(2) = #*T1*y(2)
repeat until T2(2) - T2 < 0.001

回答(1 个)

Michael Madelaire
Michael Madelaire 2017-10-24
Hi Tony
I hope this can help! Due tell if i misunderstood.
tol = 0.001; % Tolerance
y=1.4; % Set gamma value
T1 = 1; % Set T1 value
dT2 = 1; % Need to be higher than tolerance to initialize
T2 = T1*y; % First iteration
n=1; % counter
while dT2(n) > tol
n=n+1;
y(n) = (T2(n-1)+T1)/2;
T2(n) = T1*y(n);
dT2(n) = T2(n-1) - T2(n);
end
% The last iteration will exeed the tolerance
T2 = T2(1:n-1);
y = y(1:n-1);

类别

Help CenterFile Exchange 中查找有关 Performance and Memory 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by