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
2021 年 6 月 25 日
What would be the input to the Simulink block?
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
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
2021 年 6 月 25 日
manish kumar
2021 年 6 月 25 日
Walter Roberson
2021 年 6 月 25 日
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
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
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.
manish kumar
2021 年 6 月 25 日
manish kumar
2021 年 6 月 25 日
編集済み: Walter Roberson
2021 年 6 月 25 日
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
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
bestx1_partial = solve(diff(y1, x1),x1)
y1a = simplify(subs(y1, x1, bestx1_partial))
bestx2 = arrayfun(@solve, diff(y1a, x2))
bestx1 = subs(bestx1_partial, x2, 0)
y1projected = subs(y1, {x1, x2}, {bestx1, 0})
miny1 = min(y1projected)
bestx1 = bestx1(y1projected == miny1)
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 件)
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
bestx1_partial = solve(diff(y1, x1),x1)
y1a = simplify(subs(y1, x1, bestx1_partial))
bestx2 = arrayfun(@solve, diff(y1a, x2))
bestx1 = subs(bestx1_partial, x2, 0)
y1projected = subs(y1, {x1, x2}, {bestx1, 0})
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))
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)
カテゴリ
ヘルプ センター および File Exchange で Solver Outputs and Iterative Display についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!













