fsolve in Matlab function block Simulink with two outputs
5 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hi,
I formarly used the interpreted matlab function block to solve two sets of equations in two unkows; however since it is going to be removed in R2022b I am trying to modify the simulink with the matlab function block to do the same. This is what I have written:
function [m_vt_out,T_vt] = FS_sd23(P_vt,V_vt,h_conv_vt,cv_vt,Tw,gamma_vt,T_RV,m_vt)
x_init=zeros(2,1);
% Find the roots of the system of equations using fsolve
F = @(x) [P_vt * V_vt - x(1) * Rgas* 1e-6*x(2)*(1+beta*P_vt /x(2));
x(2)-(m0/x(1))^(1+ h_conv_vt *A_in/((x(1)-m_vt)*cv_vt ))*T0-...
(1-(m0/x(1))^(1+ h_conv_vt *A_in/((x(1)-m_vt)*cv_vt )))*(gamma_vt *T_RV+Tw*h_conv_vt *A_in/((x(1)-m_vt)*cv_vt))/...
(1+ h_conv_vt *A_in/((x(1)-m_vt)*cv_vt))];
x0 = [2.15;288];
options = optimoptions(@fsolve,'Algorithm','levenberg-marquardt','MaxFunctionEvaluations',1500);
[x_init,fval]= fsolve(F,x0,options);
% Return the roots
m_vt_out=x_init(1);
T_vt=x_init(2);
The problem is that I should obtain increasing values for m_vt_out and T_vt, instead at the second itaration it does not happen. This causes a negative variation, simulink stops and gives me the error: to compute complex results, make at least one input complex.
Where am I wrong?
Thanks for helping.
0 commentaires
Réponses (1)
Aishwarya Shukla
le 29 Mar 2023
Based on the code you provided, it seems that you are using the fsolve function to solve a system of two non-linear equations with two unknowns. However, you are calling fsolve only once with an initial guess x0 = [2.15;288]. This means that you are only obtaining a single solution for the system of equations, and this solution may not be valid for all input conditions.
To obtain increasing values for m_vt_out and T_vt in each iteration, you can use the previous values of m_vt_out and T_vt as the initial guess for the next iteration. You can also use a loop to iterate until convergence is achieved.
Here's an example of how you can modify your code to achieve this:
function [m_vt_out, T_vt] = FS_sd23(P_vt, V_vt, h_conv_vt, cv_vt, Tw, gamma_vt, T_RV, m_vt)
x_init = [2.15; 288]; % Initial guess for first iteration
tolerance = 1e-6; % Tolerance for convergence
max_iterations = 10; % Maximum number of iterations
for i = 1:max_iterations
% Find the roots of the system of equations using fsolve
F = @(x) [P_vt * V_vt - x(1) * Rgas * 1e-6 * x(2) * (1 + beta * P_vt / x(2));
x(2) - (m0 / x(1))^(1 + h_conv_vt * A_in / ((x(1) - m_vt) * cv_vt)) * T0 - ...
(1 - (m0 / x(1))^(1 + h_conv_vt * A_in / ((x(1) - m_vt) * cv_vt))) * (gamma_vt * T_RV + Tw * h_conv_vt * A_in / ((x(1) - m_vt) * cv_vt)) / ...
(1 + h_conv_vt * A_in / ((x(1) - m_vt) * cv_vt))];
options = optimoptions(@fsolve, 'Algorithm', 'levenberg-marquardt', 'MaxFunctionEvaluations', 1500);
[x_init_new, ~, exit_flag] = fsolve(F, x_init, options);
% Check if solution has converged
if norm(x_init_new - x_init) < tolerance
break;
end
% Use previous solution as initial guess for next iteration
x_init = x_init_new;
end
% Return the roots
m_vt_out = x_init(1);
T_vt = x_init(2);
In this modified code, a loop is used to repeatedly call fsolve until the solution converges to a tolerance specified by the tolerance variable. The x_init variable is updated with the new solution obtained by fsolve in each iteration, and this is used as the initial guess for the next iteration.
I hope this helps you to solve your problem. Let me know if you have any further questions or concerns.
Voir également
Catégories
En savoir plus sur Simulink Functions dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!