how can i write my matlab script program in simulink matlab function block

im able to run my program in matlab script
ObjectiveFunction = @simple_fitness2v;
nvars = 2; % Number of variables
LB = [0.1 0.1]; % Lower bound
UB = [5.4 8.271]; % Upper bound
ConstraintFunction = @simple_constraint2;
% [x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB, ...
% ConstraintFunction)
options = optimoptions(@ga,'MutationFcn',@mutationadaptfeasible);
% Next we run the GA solver.
options = optimoptions(options,'PlotFcn',{@gaplotbestf,@gaplotmaxconstr}, ...
'Display','iter');
% Next we run the GA solver.
[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB, ...
ConstraintFunction,options)
i write this programm in three script 1) simple fitness (objective function) 2) constraint function 3) GA call function that is shown above
How to write this program (three script of matlab) in simulink matlab function so that it will provide minimum value of output variable while running simulation

13 件のコメント

Walter Roberson
Walter Roberson 2021 年 6 月 25 日
What would be the input to the Simulink block?
HAWKINS OKEYO
HAWKINS OKEYO 2021 年 6 月 25 日
Hi kumar,
In the simulink library, go to "uder defined function", drop the matlab function then double click. It will open a file where you can define your function. just remember that the first line has to start with a function itself and ends with an "end". In other words, no comments and no variable definition outside the function itself.
In your your case, I guess you will need 3 blocks of the function.....Let me know if it works for you.
BR, Hawkins
Walter Roberson
Walter Roberson 2021 年 6 月 25 日
Note that it is valid to create a MATLAB Function Block in Simulink that does almost nothing except call MATLAB functions.
I say "almost nothing" because it will probably be necessary to initialize all output variables to the correct size and datatype.
manish kumar
manish kumar 2021 年 6 月 25 日
@Walter Roberson the input to the simulink matlab function block will be taken from my simulink model and the output will also be used in simulink model
i just want to write the code for output (optimum value) of a function ( that will depend on the two input of simulink model )
manish kumar
manish kumar 2021 年 6 月 25 日
@HAWKINS OKEYO may i get an example or script for simulink matlab code as i am low in coding?
Okay, so you take the input from your simulink model... but how does that input get useed? For example is the input going to affect the LB variable? At the moment, ga() will generate all data internally; there are no parameters for your current software.
Example: your input might give a target value to aim for, so you might want to
ObjectiveFunction = @(x)(simple_fitness2v(x) - target_value_from_input).^2
HAWKINS OKEYO
HAWKINS OKEYO 2021 年 6 月 25 日
編集済み: HAWKINS OKEYO 2021 年 6 月 25 日
what is your functional hierarchy?
In this line, why is @ga an argument to "options" function and the "options" function being an argument to "ga" function "options = optimoptions(@ga,'MutationFcn',@mutationadaptfeasible);"
clarify that then we can build it together.
Try to restructure the script again. I think I know how you want to structure it, but better do it on your own first because you understand your system better. Unless I am missing something.
Walter Roberson
Walter Roberson 2021 年 6 月 25 日
? The posted code is fine for constructing the options structure to pass to ga.
Solver name, specified as a character vector, string, or function handle.
Example: 'fmincon'
Example: @fmincon
optimoptions() calls the solver with a special parameter that tells the solver to return the default options structure. Then the other parameters of optimoptions() say how to modify the default parameters.
This is the expected usage for constructing options.
In some cases where code is being generated by MATLAB Coder or Simulink Coder, it may be necessary to hard-code all of the desired options in a single call without using any variables. This can be necessary because the code generator may use the options to decide what C++ code to generate, especially with respect to the Algorithm.
ObjectiveFunction = @simple_fitness2v;
nvars = 2; % Number of variables
LB = [0.1 0.1]; % Lower bound
UB = [5.4 8.271]; % Upper bound
ConstraintFunction = @simple_constraint2;
[x,fval] = ga(ObjectiveFunction,nvars,[],[],[],[],LB,UB, ...
ConstraintFunction)
it is the ga function
manish kumar
manish kumar 2021 年 6 月 25 日
編集済み: Walter Roberson 2021 年 6 月 25 日
y1 = (321*(x1^2 - x2^2)^(5/4))/50 - (17*x1^2)/500 - (23*x2^2)/500 - 32/6
it is my objective function where x1 and x2 are the optimized output from function block
and this the constraint function
function [c, ceq] = simple_constraint2(x)
e=4025;
c = [-800.38-x(1)^2-x(2)^2; e+0.6*x(1)*x(2)];
ceq = [];
how can i write one code in block as shown in figure with the inputs and output gets connected to simulink model output are also x1 and x2
HAWKINS OKEYO
HAWKINS OKEYO 2021 年 6 月 25 日
@manish kumar If I understand you right; given the block you just showed, x = y1, x1=m and n=x2. so, your function y1 = f(x1,x2). Is that right?
then
another function block called simple_constrain2(x) which takes 1 input which is the output of the previous function block and returns 2 outputs in the form of c and ceq. Is that what you are looking for?.......now the implementation?
@Walter Roberson do you see it that way too, or am in sort of deviating from the key idea here?
Walter Roberson
Walter Roberson 2021 年 6 月 25 日
Your inputs to the block are m and n but I do not see where they are used in the calculation ?
syms x1 x2
y1 = (321*(x1^2 - x2^2)^(5/4))/50 - (17*x1^2)/500 - (23*x2^2)/500 - 32/6
y1 = 
bestx1_partial = solve(diff(y1, x1),x1)
bestx1_partial = 
y1a = simplify(subs(y1, x1, bestx1_partial))
y1a = 
bestx2 = arrayfun(@solve, diff(y1a, x2))
bestx2 = 
bestx1 = subs(bestx1_partial, x2, 0)
bestx1 = 
y1projected = subs(y1, {x1, x2}, {bestx1, 0})
y1projected = 
miny1 = min(y1projected)
miny1 = 
bestx1 = bestx1(y1projected == miny1)
bestx1 = 
So there are two minima, at x1 = +/- 1156/64400625 and x2 = 0
These minima do not depend upon any inputs. Any inputs such as initial x1 and x2 values might slow down ga finding a minima. And it being ga(), you might only get in the neighbourhood of the best values.
If these are your functions, you should optimize to always return the known constant outputs without making the ga() call... unless the purpose of the exercise is to see how good or bad ga() is at finding known solutions.

サインインしてコメントする。

回答 (2 件)

manish kumar
manish kumar 2021 年 6 月 25 日

0 投票

@Walter Roberson m and n are the part of fitness function i put constant value in fitness function m=321 and n=500 value of these will be updated during simulation

1 件のコメント

manish kumar
manish kumar 2021 年 6 月 25 日
please can you write a combined code so that i will connect my code to function block

サインインしてコメントする。

syms x1 x2
syms m n positive
y1 = (m*(x1^2 - x2^2)^(5/4))/50 - (17*x1^2)/n - (23*x2^2)/n - 32/6
y1 = 
bestx1_partial = solve(diff(y1, x1),x1)
bestx1_partial = 
y1a = simplify(subs(y1, x1, bestx1_partial))
y1a = 
bestx2 = arrayfun(@solve, diff(y1a, x2))
bestx2 = 
bestx1 = subs(bestx1_partial, x2, 0)
bestx1 = 
y1projected = subs(y1, {x1, x2}, {bestx1, 0})
y1projected = 
Notice that all three have -16/3 . The minima is at the last two cases (+/- 462400/(m^2*n^2) ) if the term of y1projected+16/3 is positive
simplify(y1projected(2)-y1projected(1))
ans = 
but that can never be the case for positive m and n (but can be the case if n is negative)
So the optimal location is (+/- 462400/(m^2*n^2), 0) . There is no need to do a ga() search.
However, if n can be negative then the optimal location might be (0,0)

カテゴリ

質問済み:

2021 年 6 月 25 日

回答済み:

2021 年 6 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by