Variable x changes size with every loop iteration, and obtain equally spaced values. Error in lines 13 and 15
1 view (last 30 days)
Show older comments
This the code below. Errors are highlighted.
% Redlich-Kwong equation using Bisection method
%% RK-eqn setup
P = 87.3;
T = 486.9;
v = 12.005;
R = 1.98;
At = 0.0837;
Initial_x(1) = 0.5;
Last_x(1) = 2;
%% Use bisection method
for i = 1:10
Initial(i) = (R*T/(v - x)) - ((At/(v(v + x))) - R);
InitialOld = [Initial(i)];
Last(i) = (R*T/(v - x)) - ((At/(v(v + x))) - R);
LastOld = [Last(i)];
end
4 Comments
Dyuman Joshi
on 23 Oct 2023
Ok, but a general objective is not that helpful. Can you provide the context of the operations in the for loop?
Also, provide the value of "x", so we can run the code and reproduce the error to provide suggestions accordingly.
Walter Roberson
on 23 Oct 2023
That code does not change the size of x every iteration. Is the question how to have it change the size of x ? If it is, then what value should the new x value have?
Answers (1)
Walter Roberson
on 23 Oct 2023
As per request, x now changes in size every iteration, with equally spaced values.
In practice you would not write the code this way: you would just use the x values determined before the loop instead growing x every iteration.
% Redlich-Kwong equation using Bisection method
%% RK-eqn setup
P = 87.3;
T = 486.9;
v = 12.005;
R = 1.98;
At = 0.0837;
Initial_x(1) = 0.5;
Last_x(1) = 2;
xvals = linspace(Initial_x, Last_x);
x = [];
%% Use bisection method
for i = 1:length(xvals);
x(i) = xvals(i);
Initial(i) = (R*T/(v - x(i))) - ((At/(v*(v + x(i)))) - R);
InitialOld = [Initial(i)];
Last(i) = (R*T/(v - x(i))) - ((At/(v*(v + x(i)))) - R);
LastOld = [Last(i)];
end
plot(xvals, Initial(:), '-b*', xvals, Last(:), ':r.')
legend({'Initial', 'Last'})
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!