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

2 views (last 30 days)
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

Answers (1)

Michael Madelaire
Michael Madelaire on 24 Oct 2017
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);

Categories

Find more on Performance and Memory in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!