how can i write my matlab script program in simulink matlab function block
2 次查看(过去 30 天)
显示 更早的评论
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
Your inputs to the block are m and n but I do not see where they are used in the calculation ?
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
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 个)
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
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)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!