How to write a code for iteration?

59 Ansichten (letzte 30 Tage)
Felipe Gonzalez
Felipe Gonzalez am 3 Apr. 2017
Beantwortet: Rajesh am 27 Nov. 2024
Hello. I'm new using Matlab. I have to programming the below diagram in Matlab. I think I'm doing well, so far. But I've got a doubt, that is how to write a code for iteration (the step in the third block)? I guess I have to use a 'while' but I didn't know how. Hope you can help me. I let what I've done so far, in case you need it in order to answer my question. I'm writing this code with the purpose of plotting a x-y diagram and a T-x-y diagram. Thanks.
clear all; clc; close all
% Bubble temperature for P = 101.33 kPa
P = 101.33 %kPa
X = linspace (0,1,100)
phi = 1.0
x1 = X(1); x2 = X(2);
% Wilson
%PARAMETERS - System dependent
a12 = 437.98; %cal/mol
a21 = 1238.00; %cal/mol
R = 1.9872; %cal/mol?K
V1 = 76.92; %cm3/mol
V2 = 18.07; %cm3/mol
comp = 2;
% Constants for Antoine equation for 2-propane(1), water(2)
A1 = 16.6780; B1 = 3640.20; C1 = 53.54;
A2 = 16.2887; B2 = 3816.44; C2 = 46.13;
% Saturate temperature
T1sat = (B1/(A1-(log(P)))-C1);
T2sat = (B2/(A2-(log(P)))-C2);
%Temperature
T1 = sum(X*T1sat)
T2 = sum(X*T2sat)
% Activities
A12 = (V2/V1)*exp(-a12/(R*T1));
A21 = (V1/V2)*exp(-a21/(R*T2));
% Vapour pressure
P1sat = exp((A1-B1)/((T1)+C1));
P2sat = exp((A2-B2)/((T2)+C2));
for i=1:size(X,2)-1;
x1 = X(i); x2 = X(i+1);
% Calc for Gammas
ft1 = -log(x1 + 1-*A12);
st1 = x2*((A12/(x1 + x2*A12))-(A21/(x2 + x1*A21)));
gamma_1 = exp(ft1 + st1);
ft2 = -log(x2 + x1*A21);
st2 = x1*((A12/(x1 + x2*A12))-(A21/(x2 + x1*A21)));
gamma_2 = exp(ft2 - st2);
% j = Water
% Vapour pressure (Pj), kPa
Pjsat (i)= P/sum(((x2*gamma_2(i))/phi)*(P2sat/P2sat))
% Temperature, K
Tj(i) = (A2/(B2 -(log(Pjsat (i))))-C2);
% Vapour pressure, Pi_sat
Pi_sat = exp((A2-B2)/((Tj(i))+C2));
% Total pressure, P = 101.33 kPa
Ptotal = P1sat*gamma_1(i)*x1 + P2sat*gamma_2(i)*x2;
error = abs((101.33-Ptotal)/101.33);
counter = 0;
% Calc for y
y (i) = ((x2*gamma_2(i)*Pi_sat)/phi*Ptotal);
% Vapour pressure (Pj), kPa
Pj_sat = P/sum(((x2*gamma_2(i))/phi)*(Pjsat/Pjsat))
% Temperature, K
Tj=(A2/(B2 -(log(Pj_sat)))-C2);
% Iteration
if i>1
while abs(Tj(i)-Tj(i-1))<1e-10
break
end
else
end;

Akzeptierte Antwort

Thorsten
Thorsten am 4 Apr. 2017
After the first box, you write
deltaT = epsilon + 1; % so that the while loop is entered
while deltaT >= epsilon
% put code of the second box here
deltaT = % add equation for new updated value of deltaT
end
  1 Kommentar
Diyaa Khalifa
Diyaa Khalifa am 16 Dez. 2018
Do you have codes for
Bulbble P calculations and Dew T calculations and Dew P calculations ??

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Rajesh
Rajesh am 27 Nov. 2024
Hello, i am using matlab code, please give anyone correct code.I attached fractal curve . The below curve and matlab program didnot match . please give correct code and shows different iterations.
clc; clear all; close all;
format 'short';
% Input data points
x = [0 3 4 5 6]; % x-coordinates
y = [3 6 1 7 5]; % y-coordinates
% Total points in the first iteration
%total_points = 17
% Number of original intervals
iter=3 %input('Enter the number of iterations:=');
N = length(x)
p=N
% % Compute the number of interpolated points per interval
% % Ensure total points = 17 (including original points)
% points_per_interval = floor((total_points - length(x)) / N);
% remaining_points = mod((total_points - length(x)), N);
% Spline parameters
alpha = [0.5 0.8 0.7 0.6]; % Scale control parameters
r = 3 * ones(1, N); % Recurrence or tension parameters
% Derivative values
d = [5.5 -3.5 0.5 2.0 -6.0];
d1 = [2 3 4 5 6];
% Prepare X1 and Y1 matrices for the initial interpolation data
X1 = zeros(4, 4); % Initialize X1 with zeros
x1 = [x(1) x(2) x(3) x(4)];
x2 = [x(2) x(3) x(4)];
x3 = [x(1) x(2) x(3)];
x4 = [x(2) x(3)];
t = [length(x1) length(x2) length(x3) length(x4)];
X1(1, 1:t(1)) = x1;
X1(2, 1:t(2)) = x2;
X1(3, 1:t(3)) = x3;
X1(4, 1:t(4)) = x4;
Y1 = zeros(4, 4); % Initialize Y1 with zeros
y1 = [y(1) y(2) y(3) y(4)];
y2 = [y(2) y(3) y(4)];
y3 = [y(1) y(2) y(3)];
y4 = [y(2) y(3)];
t = [length(y1) length(y2) length(y3) length(y4)];
Y1(1, 1:t(1)) = y1;
Y1(2, 1:t(2)) = y2;
Y1(3, 1:t(3)) = y3;
Y1(4, 1:t(4)) = y4;
% Initialize q, L, and L1 to avoid deletion error
q = zeros(1, 5) % Pre-allocate q to avoid the error
% Initialize storage for new points
X = []; % Interpolated x-coordinates
Y = []; % Interpolated y-coordinates
% Calculate a(i) and b(i) for each interval
for i = 1:N-1
a(i) = (x(i+1) - x(i)) / (X1(i, t(i)) - X1(i, 1));
b(i) = ((X1(i, t(i)) * x(i)) - (X1(i, 1) * x(i+1))) / (X1(i, t(i)) - X1(i, 1));
end
ab_values = [a' b']
T=((x(i) - X1(i, 1)) / (X1(i, t(i)) - X1(i, 1)))
for i = 1:N-1
% Calculate term1 using the formula provided
term1(i) = (y(i) - alpha(i) * Y1(i, 1)) * (1 -T ).^3
% Calculate term2 using the formula provided
term2(i) = [r(i) * (y(i) - alpha(i) * Y1(i, 1)) + (x(i+1) - x(i)) * d(i) - alpha(i) * ...
(d1(i) * (X1(i, t(i)) - X1(i, 1)))] * (1 - T).^2 *(T)
% Calculate term3 using the formula provided
term3(i) = [r(i) * (y(i+1) - alpha(i) * Y1(i, t(i))) - ((x(i+1) - x(i)) * d(i+1)) + alpha(i) * ...
(d1(i) * (X1(i, t(i)) - X1(i, 1)))] * (1 - T) * (T).^2
% Calculate term4 using the formula provided
term4(i) = (y(i+1) - alpha(i) * Y1(i, t(i))) * (T).^3
% Compute the numerator by summing all terms
numerator = term1(i) + term2(i) + term3(i) + term4(i)
% Compute the denominator
denominator = 1 + (r(i) - 3) * (1 - T)*(T)
% Calculate q(i) as the quotient of numerator and denominator
q(i) = numerator / denominator
end
L = []; L1 = []; X = []; Y = []; %X1=[]; Y1=[]; % Initialize matrices
% Perform iterations
for k = 1:iter
fprintf("ITERATION=%d\n", k);
for i = 1:N-1
for j = 1:t(i) % Use t(i) as the column bound
if k == 1 % First iteration
fprintf('(i,j) values (%d,%d)\n', i, j);
L(i, j) = a(i) * X1(i,j) + b(i);
L1(i, j) = alpha(i) * Y1(i,j) + q(i)*(X1(i,j));
else % More than one iteration
L(i, j) = a(i) * X1(i, j) + b(i);
L1(i, j) = alpha(i) * Y1(i,j) + q(i)*(X1(i,j));
end
end
% Store new points
X = [X, L(i, 1:t(i))]; % Append only valid columns
Y = [Y, L1(i, 1:t(i))]; % Append only valid columns
end
end
XX = [X, L(i, :)];
YY = [Y, L1(i, :)];
XX= unique (XX , 'rows') ;
X1=XX ( : , 1 ); Y1=XX ( : , 2 )
[X' Y']
% % Combine original points and interpolated points
X1 = unique([x, XX]) % Include original x-coordinates
Y1 = interp1(x, y, X1, 'linear'); % Ensure all x points map to a y value
% % Display Results
disp([X1', Y1'])
% % Plot Original and Interpolated Points
%subplot(ceil(iter/2), 2, k);
%plot(X1,Y1)
% Original points
plot(x, y, '.k', 'MarkerSize', 10); hold on;
% Interpolated curve
plot(X1, Y1, 'm-', 'LineWidth', 1.5);
xlabel('x');
ylabel('y');
title('Recurrent Rational Fractal Cubic Spline');
grid on;

Kategorien

Mehr zu Thermodynamics and Heat Transfer finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by