Clear Filters
Clear Filters

using element in a vector imported from workspace in simulink

14 views (last 30 days)
###### ----------------- EDIT: I ADDED THE SIMULINK MODEL AND THE MATLAB FILE WHERE i SYNTHESIZED THE DATA
###### ----------------- EDIT2-OBJECTIVE: I'm trying to recreate the model presented in the following paper: here. I'm looking at equation (13) with k=2, and the expressions (D1) and (D2) above it. I'm also using the definition of c'(x) from equation (9) and the definition of η below equation (3).
Hi,
I don't have much experience with simulink, and I couldn't find exactly what I was looking for online, I hope someone might help:
I have a vector x which I pass to simulink from the workspace. Now, I'm trying to implement a used defined function block:
function y = fcn(lambda, x, e1, e2)
if x < sqrt(e1*lambda)
y = sqrt(e1*lambda) - x;
elseif x > sqrt((1/e2)*lambda)
y = sqrt((1/e2)*lambda) - x;
else
y = 0;
end
where lambda is calculated in the simulation in previous block (the computation is correct) and e1, e2 are scalar values of type double. I pass the parameters from the workspace as follows:
x_data.time=t';
x_data.signals.values = x';
e1_data.time=t';
e1vec = repelem(e1, length(x));
e1_data.signals.values = e1vec';
e2_data.time=t';
e2vec = repelem(eta_dis, length(x));
e2_data.signals.values = e2vec';
I get the following error:
Index exceeds array dimensions. Index value 0 exceeds valid range [1-1] for array 'x'.
Error in 'pmp_sim/MATLAB Function' (line 3)
x = x(t);
I thought the a problem is that simulink treats these vectors as one block or something, as I read online, when I try to treat these elements in the function as vectors, meaning:
function y = fcn(lambda, x, e1, e2)
x = x(t);
e1 = e1(t);
e2 = e2(t);
if x < sqrt(e1*lambda)
y = sqrt(e1*lambda) - x;
elseif x > sqrt((1/e2)*lambda)
y = sqrt((1/e2)*lambda) - x;
else
y = 0;
end
where t is the simulation clock, I get the following error:
Domain error. To compute complex results from real x, use 'sqrt(complex(x))'.
Error in sqrt.m (line 13)
coder.internal.error('Coder:toolbox:ElFunDomainError',mfilename);
Error in 'pmp_sim/MATLAB Function' (line 8)
can someone please explain to me how should I treat these vectors that I import to simulink model from workspace?
Thank you for you time and attention.
  2 Comments
Ashutosh Thakur
Ashutosh Thakur on 22 Jul 2024
Hi Elinor,
The error message in the end suggests that Complex number is getting evaluated and root cause cannot be known without model and the data which is passed to it. Could you also share the model and data as well?
Thanks.
Elinor Ginzburg
Elinor Ginzburg on 22 Jul 2024
Hi @Ashutosh Thakur, thank you for your response!
I added all the files, please note that I'm trying to recreate the result from a published paper.
If you could give me any insight why I'm having trouble running the simulink model I'll be very grateful.
Thanks again for your help.

Sign in to comment.

Answers (1)

Milan Bansal
Milan Bansal on 24 Jul 2024
Edited: Milan Bansal on 24 Jul 2024
Hi Elinor Ginzburg,
As per my understanding, you are facing an error while calculating the square root in a custom function in your Simulink model.
From the attached Simulink model and script, the following errors can be observed:
  • The first mentioned error is probably due to the variable "x" being a "struct" and it being indexed. Get the values in "x" using 'dot' and then use indexing (example: "x.values(t)").
  • It can be noticed that you are using a "clock" block to get the time instance "t" in your function block, and you are using this "t" for indexing your "pl", "eta_chg" and "eta_dis" variables. The value of "t" can be a non-integer value and will throw the below error when used for indexing. The fix is to use integer values for indexing instead of "t".
  • You are using workspace variables "pl_data", "eta_chg_data", and "eta_dis_data" from workspace in the function block in the Simulink model. These variables are actually "structs" as per the attached script, and they cannot be indexed directly as being done in the function block. Instead, use the "pl", "eta_chg_vec", and "eta_dis_vec" variables, which are also present in the workspace and contain an array of values that can be indexed directly.
  • The values in the above-mentioned variable are all positive, as generated by the attached script, whereas the "lambda_star" in the function block may take a negative value. Hence, calculating the square root of (eta_chg * lamda_star) is not possible directly due to the negative product. If you wish to calculate the complex square root, then it is required to typecast the product into complex as shown in the code snippet below:
sqrt(complex(eta_chg*lambda_star))
Hope this helps!
  1 Comment
Elinor Ginzburg
Elinor Ginzburg on 29 Jul 2024 at 6:35
Hello Milan,
Thank you very for your detailed explanation. I followed the remarks you pointed out, but unfortunately I get new errors due to the changes I entered. For some reason, it has problem with
sqrt(complex(eta_chg*lambda_star))
and it also doesn't recognize the values fields of the structs. I also tried signals.values, but it still doesn't recognize it, and I'm not sure why. Additional change I enetered is to use round() on the clock ticks, to make sure it's an integer.
If you have any ideas why it doesn't recognize the structs I'll be happy to hear. I'll add the updated files to this comment during the day, perhaps I wrote something inccorectly.
Thank you again for your time and advice.

Sign in to comment.

Categories

Find more on Simulink Functions in Help Center and File Exchange

Products


Release

R2024a

Community Treasure Hunt

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

Start Hunting!