Variable x changes size with every loop iteration, and obtain equally spaced values. Error in lines 13 and 15
조회 수: 1 (최근 30일)
이전 댓글 표시
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
Dyuman Joshi
2023년 10월 23일
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
2023년 10월 23일
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?
답변 (1개)
Walter Roberson
2023년 10월 23일
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
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!