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

2 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 25 Jun 2021
Your inputs to the block are m and n but I do not see where they are used in the calculation ?
Walter Roberson
Walter Roberson on 25 Jun 2021
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.

Sign in to comment.

Answers (2)

manish kumar
manish kumar on 25 Jun 2021
@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

Walter Roberson
Walter Roberson on 25 Jun 2021
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)

Categories

Find more on Optimization in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!