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

조회 수: 1 (최근 30일)
manish kumar
manish kumar 2021년 6월 25일
답변: Walter Roberson 2021년 6월 25일
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일
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 2021년 6월 25일
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일
@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

댓글을 달려면 로그인하십시오.


Walter Roberson
Walter Roberson 2021년 6월 25일
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)

카테고리

Help CenterFile 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!

Translated by