How to call a function from simulink, using a matlab function block?

4 ビュー (過去 30 日間)
Jesper Ankersen
Jesper Ankersen 2021 年 3 月 11 日
回答済み: Jesper Ankersen 2021 年 3 月 11 日
Hello I need youre help. My question is simple and here it goes:
How can i call a matlab function i made from inside a simulink block?
The matlab (Pos_Solve_Allo.m) function looks like this:
function fp = Pos_Solve_Allo(M, F_gen)
%Pos_Solve_Allo.m
lb = [0;0;0;0;0;0;0;0];
ub = [Inf;Inf;inf;Inf;Inf;inf;Inf;Inf]; % lower and upper bounds
% Aeq = [0 1 4/10;1 0 7/5];
% beq = [7/20;2/5];
fp = lsqlin(M,F_gen,[],[],[],[],lb,ub)
end
So inside the function i use lsqlin solver, which cannot be used in matlab function block as is, that is why i included it in the function above.
Inside my matlab function block i call Pos_Solve_Allo in the following way:
function fp = fcn(m)
% Konstants
a = (1/2 + 1/sqrt(12));
b = (1/2 - 1/sqrt(12));
c = (1/sqrt(3));
P = 1/sqrt(3)*[1 -1 1 -1 1 -1 1 -1;
1 1 -1 -1 1 1 -1 -1;
1 1 1 1 -1 -1 -1 -1];
X=[-a b -b a a -b b -a;
b a -a -b -b -a a b;
c -c -c c c -c -c c];
M = [X;cross(P,X)]
F_gen = transpose([0 0 9.8*m 0 0 0]);
Pos_Solve_Allo(M,F_gen) %Here I call the function
end
However i get the following error:
Function 'lsqlin' not supported for code generation.
Function 'Pos_Solve_Allo.m' (#592.199.232), line 7, column 6: "lsqlin(M,F_gen,[],[],[],[],lb,ub)" Launch diagnostic report.
Component:MATLAB Function | Category:Coder error
Function call failed.

採用された回答

Jesper Ankersen
Jesper Ankersen 2021 年 3 月 11 日
I found a solution to the problem, and in the pictures u can see how it is solved. There exists similar discussion on the topic as well in the matlab questions forum, but heres is how i ended up solving the issue:

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear Least Squares についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by