Variable x changes size with every loop iteration, and obtain equally spaced values. Error in lines 13 and 15

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

"x" has not been defined. So we can't run your code to reproduce the errors.
Also, it would be better if you can specify what the code is written to do.
My bad. The code I have written is to compute x in Redlich-Kwong equation using bisection method.
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.
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?

Sign in to comment.

Answers (1)

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'})

Asked:

on 22 Oct 2023

Answered:

on 23 Oct 2023

Community Treasure Hunt

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

Start Hunting!