필터 지우기
필터 지우기

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

카테고리

Help CenterFile Exchange에서 Pulse and Transition Metrics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by