plotting using fsolve in a for loop (2 variables)

I want to plot two equations in terms of one parameter z that goes from 0.1 to 1. For that, I used fsolve in a for loop. When I run my code, it says that the equation is solved but there is no line line on my graph.
Here is my code:
P_1;
k=25;
a=-0.0075;
b=0.1750;
c=4;
z=linspace(0.1,1);
for z=1:length(z);
f@(x) compressorcharacteristic(x, z, k, P_, a, b, c);
xguess=[1.1,10];
sol=fsolve(f,xguess);
mdot_sol=sol(1);
P_sol=sol(2);
end
plot(z,mdot_sol);
hold on
plot(z,P_sol);
hold off
function y=compressorcharacteristic(x,P_,k,a,b,c,z)
mdot=x(1);
P=x(2);
y(1)=z.*k.*sqrt(P-P_)-mdot;
y(2)=a.*(mdot.^2)+b.*mdot+4-P;
end
Can someone pinpoint the problem ?

 Réponse acceptée

I had to guess the value of P_
Your equations appear to have complex-valued solutions
P_ = 1;
k=25;
a=-0.0075;
b=0.1750;
c=4;
opts = optimoptions('fsolve', 'display', 'none');
zvals = linspace(0.1,1);
Nz = length(zvals);
mdot_sol = zeros(Nz, 1);
P_sol = zeros(Nz, 1);
for zidx = 1 : Nz
z = zvals(zidx);
f = @(x) compressorcharacteristic(x, z, k, P_, a, b, c);
xguess=[1.1,10];
sol = fsolve(f, xguess, opts);
mdot_sol(zidx) = sol(1);
P_sol(zidx) = sol(2);
end
subplot(2,2,1)
plot(zvals, real(mdot_sol)); title('mdot - real');
subplot(2,2,2)
plot(zvals, real(P_sol)); title('P - real');
subplot(2,2,3)
plot(zvals, imag(mdot_sol)); title('mdot - imaginary');
subplot(2,2,4)
plot(zvals, imag(P_sol)); title('P - imaginary');
function y=compressorcharacteristic(x,P_,k,a,b,c,z)
mdot=x(1);
P=x(2);
y(1)=z.*k.*sqrt(P-P_)-mdot;
y(2)=a.*(mdot.^2)+b.*mdot+4-P;
end

1 commentaire

Nora
Nora le 19 Jan 2023
You got it right for the P_ value, sorry I forgot to put an "=". Anyway, thank you so much !

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Programming dans Centre d'aide et File Exchange

Produits

Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by