How can I calculate block parameters from mask parameters?

조회 수: 4 (최근 30일)
Annika Michel
Annika Michel 2023년 11월 21일
편집: Voss 2024년 10월 30일
The Goal:
I have a subsystem which I now want to control over a mask. The mask should allow me to chose between two materials and give a thickness and radius of a cylinder. In the subsystem I then want to use the material choice to define the relevant material parameters (e.g. in a "if material == 1, then parameters =... " statement) and I want to pass on the thickness and radius, however they are not direct parameters for a block in the code (so I can't just promote them). Instead I want to do calculations with them, e.g. I have an impedance that calculates from Z=rho*pi*r^2. Rho should be defined by the Material choice and r should be given in the mask in an edit block. See below for what I picture for the mask:
Now what I need to calculate from these is some further values. Before I created the subsystem I did this in the InitFunc of the model.
What I have currently:
classdef piezoElement_mask
methods(Static)
% Following properties of 'maskInitContext' are available to use:
% - BlockHandle
% - MaskObject
% - MaskWorkspace: Use get/set APIs to work with mask workspace.
function MaskInitialization(~)
end
% Use the code browser on the left to add the callbacks.
function material(mat)
if isequal(mat, 1)
g33=0.0142188;
g31=-0.0333414;
c33D=1.362e11;
c11D=7.268e10;
h33=g33*c33D;
h31=g31*c11D;
eps33S=9.249e-9;
rho=14672.908;
end
if isequal(mat,2)
c33D=1.295e11;
c11D=8.587e10;
h33=1.302e9;
h31=-1.684e9;
eps33S=8.711e-9;
rho=17435.538;
end
end
function thickness(th)
d = th*1e-3;
end
function radius(rd)
r = rd*1e-3;
A=pi*r^2;
C0=A*eps33S/d;
Z33 = A*sqrt(c33D/rho)*rho;
t33=d/sqrt(c33D/rho) ;
Z11 = 2*pi*d*r*sqrt(c11D/rho)*rho;
t11=2*r/sqrt(c11D/rho) ;
end
end
end
There's probably a better way to do this, also this right now doesn't work. I always get an error for thickness and radius (material seems to be fine?) and my blocks in the subsystem still use the parameters stored in the MATLAB variable browser, not the updated values I try to give here.
Can anyone help? I've found loads of information on passing on parameters, but only for when you use the parameter directly and not if you want to use them for calculations first.

채택된 답변

Angelo Yeo
Angelo Yeo 2023년 11월 22일
편집: Angelo Yeo 2023년 11월 22일
If you were to choose one our of multiple scenarios depending on your choices, a good way to go is to use a variant subsystem with masking. The example below can be of your help.
  댓글 수: 8
Angelo Yeo
Angelo Yeo 2023년 11월 23일
Thank you Annika for your reply. I'm glad you made some progress :D
BLOT: Change the code like below.
function rad(~)
radius = get_param(gcbh, 'rad');
radius = str2double(radius); % Add this %
disp(radius)
disp(radius-48)
assignin('base', 'r', radius*0.001);
end
Note that the variable radius is a character variable. I believe you must have taken this value from "Edit" field. Characters are basically numbers and mapped by ASCII rule. So, MATLAB dynamically mapped back the character into original number. For example, the character 'A', 'B', 'C' are mapped from 65, 66, 65 respectively.
double('A')
ans = 65
double('B')
ans = 66
double('C')
ans = 67
Accordingly, the character '3' is mapped from is mapped from 51.
double('3')
ans = 51
That's why you would get the result 3 when you calculated '3' - 48.
'3'-48
ans = 3
Same story for '2.5'. Let's see how '2', '.', '5' are mapped from 50, 46, 53 in ASCII respectively.
double('2.5')
ans = 1×3
50 46 53
So that's why
'2.5' - 48
ans = 1×3
2 -2 5
So, the solution is to use str2double. This will change characters into the number (not mapping back to numbers based on ASCII rule).
Annika Michel
Annika Michel 2023년 11월 24일
Thank you, that fixed it!
Thanks again for everything!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Annika Michel
Annika Michel 2023년 11월 22일
I've attached the model, subsystem and subsystem mask. In the Model (Galliere_neg_Kond_V16_TEW_ref_bauteil) you will find a bunch of calculations in the InitFcn. The first half of those I would like to do within the referenced subsystem, but haven't found a way to do so yet. If you look at the subsystem (PiezoElement) you will find that all block parameters within are variables, which currently are defined in the Model's InitFcn. However, I would like to contain those variables within the subsystem by creating a system mask. The system mask would allow me to chose between two materials (second material is currently not within the Model InitFcn, but would work equivalent with different values) and give decimal values for radius and thickness. With the material parameters and radius and thickness I want to then calculate the variables given into the subsystem blocks. Again, this should all happen within the subsystem. The model should only see the mask.
The subsystem mask (piezoElement_mask) is my attempt to execute calculations with the mask input parameters. However, the variables I define within the mask code do not overwrite the subsystem variables and do not get passed on to the subsystem blocks.
What I want:
1) Subsystem is in Model. Model shows subsystem Mask. Mask contains Material choice (1 or 2), radius (edit field, numerical), thickness (edit field, numerical)
2) Subsystem recieves (e.g.) Material =1; radius = 3; thickness =5.
3) Subsystem goes through calculations and definitions: If material is =1, then Material parameters are defined according to list 1. Radius and Thickness and material parameters are used in several calculations for 9 different block parameters.
4) Subsystem passes calculation results on to blocks.
I'm having trouble with 2->3 and 3->4, basically I don't know how to access the mask parameters and do things with them.
  댓글 수: 1
Annika Michel
Annika Michel 2023년 11월 22일
Oh and for the Model to run you need to define freqHz in the console. A good value is freqHz=280000

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Subsystems에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by