Filter löschen
Filter löschen

How to insert a function handle in simulink !?

1 Ansicht (letzte 30 Tage)
Suppenhuhn
Suppenhuhn am 10 Mär. 2011
Hi,
I wrote a function f(x)=a*x(1)+b*x(2) in Matlab (without Simulink) as an .m-file and let ga(@function,2) calculate an optimum for given a,b. My next step is to insert that situation into Simulink - so, that a closed loop delivers different a,b with every step. How can I now implement my genetic algorithm (ga(@function,2)) ?
I actually wrote the function as s-function (with two inputs for a,b). Do I maybe have to write an Block Property/Callback with the command ga(@function,2) or how can I place my gen. algo. !?

Antworten (2)

Guy Rouleau
Guy Rouleau am 11 Mär. 2011
In a Level 2 MATLAB S-function, you should be able to use your function in the same way as you do in MATLAB.
Do you receive any error?

Suppenhuhn
Suppenhuhn am 11 Mär. 2011
Well - it doesn´t work !? I´m just a bit confused how to insert the order, that I (from pure Matlab) would enter from Workspace. In my Matlab-version I just write evalfis([5 7],[fisdata]) in my .m-function and the optimal Minimum for my function (f(x)=a*x(1)+b*x(2)) is getting calculated by writing ga(@function,2)(for given a,b (=5,7)). But I don´t know how to insert the ga-command in a Simulink-loop, where a and b are getting constantly changed. As I didn´t know better, I wrote a "Letsgo.m"-file with ga(@function, 2) and for my s-function I added a Callback in Block Properties/OpenFcn with the command set_param('Letsgo').
The resulting error is: "Error evaluating registered method 'Outputs' of M-S-Function 'testi_s' in 'Trink_s/Level-2 M-file S-Function'. The following error occurred converting from double to opaque: Error using ==> horzcat Undefined function or method 'opaque' for input arguments of type 'double'. The following is the MATLAB call stack (file names and line numbers) that produced this error: ['C:\...\...\...\testi_s.m'] [126]"
Line 126 is the first line in my s_function that has the parameter a or b !?

Kategorien

Mehr zu Simulink finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by