implementation of the bisection method
3 ビュー (過去 30 日間)
古いコメントを表示
Hi everyone,
I'd like to use the bisection method to find the V values that will make my set of functions equal to zero (V_ls).
func=@(V) -Pstar+(R*T*(((1-c)/V)-((c/b)*log(1-b/V)))-a*c/V^2)
note that a,b,c,R retain always the same value, the difference that makes my set of equations is the values of Pstar,T that will then determine the V value that I'm looking for.
I successfully managed to do this process for one given function, by setting the value of Pstar and T.
I'd like to be able to repeat this process automatically for different values of Pstar and T that are stored in a vector.
Pstar=Pstars(i)
T=temp(i)
%i have 30 values (given by the problem) in each vector, i.e. i=30
%range values where the solution can be found.
V=0.0320;
lim_max= 0.1951;
%bisection method
number_iterations= 100 % i can set this value arbitrarily
for i=1:number_iterations
Vls=(V+lim_max)/2;
if fun(Vls)<0
lim_max=Vls;
else
V=Vls;
end
end
I suppose I should put this for cicle into a new one, but I really cant manage to get the results I want:
an output vector V_ls that contains each solution given by the bisection method for each of my 30 equations.
Many thanks !
4 件のコメント
Torsten
2022 年 11 月 5 日
Yes, I understand this. But the code above does not work for one pair. How should I change it for 30 pairs then ?
If you want to try it on your own:
Make a function "bisection" that is called with the necessary inputs (temp(i),Pstar(i),fun,maximum number of iterations,...) and returns V(i). Then call this function in a loop like:
for i=1:30
Vsol(i) = bisection(temp(i),Pstar(i),fun,Vinit,max_iter,...);
end
採用された回答
Torsten
2022 年 11 月 5 日
編集済み: Torsten
2022 年 11 月 5 日
%fixed inputs
c=25.8972;
b=0.0320;
a=0.7535;
R=0.0821;
Vc=0.1951;
% Generate 30 artificial (Pstar,T) pairs to test
Pstar_array = 36.6133*(1.05).^(1:30);
T_array = 385.15+(1:30);
% Call bisection for 30 (Pstar,T) pairs
for i=1:numel(T_array)
Pstar = Pstar_array(i);
T = T_array(i);
fun = @(V) -Pstar+(R*T*(((1-c)/V)-((c/b)*log(1-b/V)))-a*c/V^2);
V(i) = bisection(fun,b,Vc);
error(i) = fun(V(i));
end
figure(1)
plot(1:30,V)
figure(2)
plot(1:30,error)
function sol = bisection(fun,V0,Vc)
n=100;
V=V0;
limmax=Vc;
for i=1:n
Vls=(V+limmax)/2;
if fun(Vls)<0
limmax=Vls;
else
V=Vls;
end
end
sol=Vls;
end
0 件のコメント
その他の回答 (0 件)
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

