implementation of the bisection method
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Ivan De Fazio
el 5 de Nov. de 2022
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 comentarios
Torsten
el 5 de Nov. de 2022
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
Ivan De Fazio
el 5 de Nov. de 2022
Editada: Ivan De Fazio
el 5 de Nov. de 2022
Respuesta aceptada
Torsten
el 5 de Nov. de 2022
Editada: Torsten
el 5 de Nov. de 2022
%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 comentarios
Más respuestas (0)
Ver también
Categorías
Más información sobre Desktop en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

