modelling a resonator in simulink - simulink

I have been trying to model the Fabry-Perot resonator in simulink. I am not sure if it is right to choose simulink for this task but I have been getting some results, at least. However, I have been also getting an error of algebraic loop when I use a different pair of coupling/reflection parameters. It says,
"Simulink cannot solve the algebraic loop containing
'jblock_multi_MR/Meander2b/Subsystem3/Real-Imag to Complex' at time
6.91999999999991 using the LineSearch-based algorithm due to one of the
following reasons: the model is ill-defined i.e., the system equations do
not have a solution; or the nonlinear equation solver failed to converge
due to numerical issues.
To rule out solver convergence as the cause of this error, either
a) switch to TrustRegion-based algorithm using
set_param('jblock_multi_MR','AlgebraicLoopSolver','TrustRegion')
b) reducing the VariableStepDiscrete solver RelTol parameter so that
the solver takes smaller time steps.
If the error persists in spite of the above changes, then the model is
likely ill-defined and requires modification."
Changing the solver does not help. As a note, I implemented the system in terms of electric fields and complex signals naturally.
thanks for any help.

There is no magic solution for solving algebraic loop issues, as these issues tend to be very model-dependent. Here are a few pointers though:
What are algebraic loops in Simulink and how do I solve them?
How can I resolve algebraic loops in my Simulink model in Simulink 6.5 (R2006b)?
Algebraic Loops in the Simulink documentation
See also this answer to a similar question on SO, with some suggestions for breaking the loop.

Related

How to deal with failure to solve nonlinear equations during simulation in Dymola?

I build a model of solar power plant with the Rankine cycle in Dymola, even though the initialization works fine, but at the time of 2840s, there is a failure to solve the nonlinear equations which leads the simulation to stop.
As shown in the following screenshot, Dymola recommends giving better start values, but it doesn't make any sense, because the failure happens during the simulation instead of initialization. And with better start values, the situation doesn't improve at all.
My question is :
How should I deal with the failure to solve nonlinear equations during simulation?
The error message indicates that the solver was not able to find a solution to a nonlinear system of equations in your model. This could mean either that there is no solution to the system or just that the solver is not able to find it.
The first easy steps would be to try a lower tolerance in simulation settings. And to use another solver that might suit better for your problem. Dassl can handle nonlinear systems quite well. If these steps won't work it might be that there just isn't a solution to your problem and you made a wrong assumption or accidentially have a wrong parameter somewhere.

Managing of Navier-Stokes PDEs by means of SBF in Dymola

Has anyone tried to implement the Navier Stokes Partial Differential Equations (PDE) in Modelica?
I found the method of the spatial basis functions (SBF) which by means of numerical modifications gets Ordinary Differential Equations (ODE) that could be handled by Dymola.
Regards,
Victor
The aim of the method I was saying before is to convert PDEs in ODEs, so the issues with the CFL coefficient would disappear, the problem is that the Modelica.Fluids elements just define the equations in function of the variables in both ends of each component.
i.e dp=port_a.p-port_b.p
but with that sort of methodology, the variables such as pressure, density, mass flow... would be function also of the surrounding components... it would be a kind of massive interaction between all the components,
I would like to see an example in Modelica, because I hardly haven't found information about that topic linked to Modelica.
Modelica is a language for modeling behavior described by DAEs. As such, as long as you can create a system of ODEs, you should be able to express your problem in Modelica.
However, if your PDEs are hyperbolic, the wave dynamics in the equations might cause some issues with simulation. This is because the CFL condition imposes limits on time steps that an ordinary differential equation solver will be unaware of. If the solver includes error control, it will probably manage to get a solutions but may run quite slow because it won't know how to explicitly limit the simulation step size. If it doesn't include error control and it violates the CFL condition, the system will go unstable. Note, this only applies to systems where the CFL condition applies.

Fit data with a numerical ode solution (on Matlab)

I am looking for a way to fit my experimental data by a theoretical model which is described by a non-linear differential equation.
Unfortunately this latter can only be solved numerically (by solving this second degree, non-linear differential equation).
I manage to solve the differential equation for a set of parameters using the ode45 Matlab solver but now I want to find the proper fit parameters of the model. Also, I may have to mention that my ode45 is initiated at z=zmax (max being large so I can assume it is infinity) by y(zmax)=y0 and yprime(zmax)=yprime0 and I solve backward (from zmax to z=0).
I am quite new to this kind of numerical problems, are there classical ways to solve such problems?
Does anyone knows if there is a Matlab procedure which would help me solve this? On which principles is it based/constructed? (if possible I'd like to know the theoretical trick to solve this problem in a smart way, not by trying all the possible sets of parameters which would be very time consuming (I have 5 fit parameters!).
Thank you for your precious help!
You have facy methods in the Optimization Toolbox. In case you don't have access to it, you could do it manually by:
Selecting a cost function between the experimental and model data. For example, mean-squared-error.
Doing heuristic optimization of the cost function. For example, Nelder-Mead method.

Embedded MATLAB function : Block Error

I made a design in simulink to implement PID using embedded matlab function.
My function is :
function [u,integral,previous_error] = fcn(Kp,Td,Ti,error,previous_error1,integral1)
dt = 1;
Ki= Kp/Ti;
Kd=Kp*Td;
integral = integral1 + error*dt; % integral term
derivative = (error-previous_error1)/dt; % derivative term
u = Kp*error+Ki*integral+Kd*derivative; % action of control
previous_error=error;
%integral=integral;
end
This is how my model looks:(a part of the entire model)
I am getting the following error :
Simulink cannot solve the algebraic loop containing 'pid_block1/MATLAB Function' at time 2.2250754336053813E-8 using the TrustRegion-based algorithm due to one of the following reasons: the model is ill-defined i.e., the system equations do not have a solution; or the nonlinear equation solver failed to converge due to numerical issues.
To rule out solver convergence as the cause of this error, either
a) switch to LineSearch-based algorithm using
set_param('pid_block1','AlgebraicLoopSolver','LineSearch')
b) reducing the ode45 solver RelTol parameter so that the solver takes smaller time steps.
If the error persists in spite of the above changes, then the model is likely ill-defined and requires modification.
Any idea, why am I getting it?
Should i use global variables for integral and previous_error here?
Thanks in advance.
Erm.. Unless there is a specific reason why you need it in this form, I would strongly recommend replacing your MATLAB Function block with Simulink blocks such as:
Gain blocks for Kp, KI, and KD
Sum blocks for all addition and subtraction
Derivative block for derivative
Integrator block for integration
etc....
I've found that Algebraic loop problems are really hard to get rid of and are usually just best to avoid. The method I suggest above can be used for most any controller type and has worked quite nicely for me in the past.
If the issue is neatness, you can always create your own "PID controller" subsystem or library part.
Let me know if you need some more detail or a diagram on how you might do this.

Resolving problems with Algebraic Loops in SIMULINK models

MY PROBLEM
I have a SIMULINK model that has got a feeback loop aka Algebraic Loops that is causing error in the simulation. My original solver configuration was Fixed-Step with Dormand-Prince (Order 5). I also tried Fixed-Step with Order 3 (Runga-Kutta). It still has not resolved the issue.
MY QUESTION
Is there any way to resolve algebraic loop without altering the original performance or design of the circuit too much?
CLARIFICATION FOR THE QUESTION
I am sure that there will be a way to solve this. However, I don't want it to compromise the original performance of the circuit. Moreover, it is a customer-supplied data and it seems that they have managed to work this out as fine. I simply require somebody to point me to the right direction regarding how to solve this.
MY Approach so far
I tried to break the loop using:
i) Switch (if ip = 0, op = 0; if ip = 1, op = 1) I know it is stupid but it is a different block that breaks the loop.
ii) Logic gate (XORing the feedback signal with 0).
Unfortunately, I don't know how to do a zero-order hold unit delay loop that seems to be another commonly prescribed solution for this kind of problem. But I believe that may cause issues with my model's originally intended performance.
I have posted a query on Mathworks website, but no response so far. SO I thought...why not stackoverflow? Below is the image.
Add a unit delay block on the feed back signal (from Logical Operator1 to Logical Operator4).
The unit delay provides the previous value of the output signal - this won't affect most of the circuitry in this scenario.
There isn't a "one size fits all" answer when it comes to algebraic loops. Here are a few resources about algebraic loops:
What are algebraic loops in Simulink and how do I solve them?
How can I resolve algebraic loops in my Simulink model in Simulink 6.5 (R2006b)?
Algebraic Loops in the Simulink documentation
and there are many others...
In your case, I would suggest highlighting the algebraic loop (as per the doc in the hyperlink above), and try inserting a unit delay in the loop. The doc shows how to do this. Other suggestions would be to try the algebraic loop solver or model parameters related to algebraic loops, or placing an IC or Algebraic Constraint block in the loop. Again, refer to the doc in the hyperlink above for details. I assume you are constrained to use a fixed-step solver and cannot switch to a variable-step solver.