User defined capacitor model in simulink

4 visualizaciones (últimos 30 días)
성주 김
성주 김 el 9 de Mayo de 2023
Respondida: Shreshth el 26 de Sept. de 2023
Hello, I would like to model a non-linear capacitor using user defined block in simulink.
Originally, simscape component used ssc code, but the simscape language does not support arc-cosh(acosh), so we would like to implement it using user defined block.
For your information, I've also used the form of acosh(x) = log((x) + sqrt((x)^2 - 1) in ssc code, but simulink can't solve the equation, so I want to use a block that supports acosh.
By the way, there are various types of user defined blocks.
First of all, I tried matlab function, but it is difficult to use node voltage because it does not support electrical node.
I would appreciate it if you could recommend user defined block (ex. S funciton C function and son) that can implement the ideal capaciotr code written in the following ssc code as much as possible. (acosh and node voltage should be available)
Ideal capacitor in ssc.
component Ideal capacitor
% Ideal Capacitor
% Models an ideal (lossless) capacitor. The output current I is related
% to the input voltage V by I = C*dV/dt where C is the capacitance.
nodes
p = foundation.electrical.electrical; % +:top
n = foundation.electrical.electrical; % -:bottom
r = foundation.electrical.electrical; % -:bottom
end
parameters
C = { 1e-6, 'F' }; % Capacitance
end
variables
i = { 0, 'A' }; % Current
v = {value = { 0, 'V' }, priority = priority.high}; % Voltage
end
branches
i : p.i -> n.i; % Current through from node p to node n
end
equations
assert(C > 0)
v == p.v - n.v; % Voltage across between node p and node n
i == C*v.der; % Equation defining the capacitor action
end
end
Furthermore, I would appreciate it if you could convert the above idea capacitor code from the block you recommended to an implementable code and present it.

Respuestas (1)

Shreshth
Shreshth el 26 de Sept. de 2023
Hello 성주 ,
In order to work with electrical nodes in Simulink, you can use S function block instead of MATLAB function block. The S-Function block does not directly support the acosh function. However, you can still implement the acosh function using standard C/C++ functions within the S-Function block.
Here's an example of how you can implement the acosh function within the S-Function block:
#include "simstruc.h"
#include <math.h>
static double acosh_custom(double x) {
return log(x + sqrt(x*x - 1));
}
static void mdlOutputs(SimStruct *S, int_T tid) {
// ...
// Calculate acosh
double acosh_val = acosh_custom(v);
// ...
}
In this example, the acosh_custom function is a custom implementation of the acosh function using the “log” and “sqrt” standard C/C++ functions.
You can include this acosh_custom function within the mdlOutputs method of the S-Function block. Replace the // ... sections with the appropriate code for your specific implementation. Remember to update the mdlInitializeSizes method to indicate the number of input and output ports your S-Function block requires.
By implementing the acosh function within the S-Function block, you can use it in your model to achieve the desired behavior.
Please go through the below S block documentation link for more information.
Hope it helps.
Thank you,
Shubham Shreshth.

Categorías

Más información sobre Simscape 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!

Translated by