Azzera filtri
Azzera filtri

fsolve in Matlab function block Simulink with two outputs

7 visualizzazioni (ultimi 30 giorni)
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.

Risposte (1)

Aishwarya Shukla
Aishwarya Shukla il 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.
  1 Commento
Roberto Tascioni
Roberto Tascioni il 2 Apr 2023
Thank you for your answer.
I tried also with different values of x0, adding a loop, but it didn't solve the error. I understood the reason why it gives me uncorrent values is in the algorithm used by fsolve. Let me explain: in my previous model, fsolve in the interpolated matlab function block finds the parameters by using the trust-region algorithm, which gives expected outcomes. In the new model I am trying to implement with the matlab function block, matlab requires me to use the Levenberg-Marquardt Algorithm. However, this type of algorithm doesn't seem to find the correct solutions. Therefore I am forced to modify the simulink because the interpreted matlab function will be removed, at the same time I cannot use the same solution method in fsolve. The one suggested doesn't work either in my previous-working model. What can I do?

Accedi per commentare.

Categorie

Scopri di più su Event Functions in Help Center e File Exchange

Prodotti


Release

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by