Objective function of a Genetic Algorithm - matlab

This is my first effort about AI/ML.
I have the following problem given by my teacher,
Design a simple genetic algorithm in MATLAB, with binary-coded chromosomes, in
order to solve pattern finding problem in 16-bit strings.
The objective function is given by the following formula:
F(x) =
NoS("010") + 2NoS("0110") + 3NoS("01110") + 4NoS("011110") +
5NoS("0111110") + 6NoS("01111110") + 7NoS("011111110") +
6NoS("0111111110") + 5NoS("01111111110") + 4NoS("011111111110") +
3NoS("0111111111110") + 2NoS("01111111111110") +
NoS("011111111111110")
I couldn't understand the formula.
What does it mean by Nos?
What operations are they performing on those binary strings?
What is F(x)?

From what I can read, the NoS are Nitric oxide synthase like NoS1 or NoS2. They are some genes of different species.
These are probably referring to minimize functions: https://www.mathworks.com/help/gads/examples/coding-and-minimizing-a-fitness-function-using-the-genetic-algorithm.html
I hope I'm helping.

Related

Matlab simplify - why does it remove fractions?

I am recently working with Matlab's Symbolic Toolbox, but encountered a big problem. I have some example code:
syms s w;
G = 0.1253/(s^4 + 0.5816*s^3 + 1.1691*s^2 + 0.4048*s + 0.177)
L1=diff(G)
L2=subs(L1,s,-s)
M2=subs(G,s,-s)
Ts=(-1/2)*((L1/G)+(L2/M2))
T=subs(Ts,s,1i*w)
Ts = simplify(T)
Everything is working well, except for the part with T=subs(Ts,s,1i*w) and T=subs(Ts,s,1i*w). In order to calculate i as i^2 = -1, I have to use simplify, but then all of the fractions disappear.
Is there any other function to do what I need? Or am I doing something wrong?

Solving the system of non-linear equations in MATLAB by fsolve

I have a code that produces a vector in MATLAB, for example the following is a three component vector (n=3):
a1_1 - sin((17*a1_1)/60 + a2_1/8 + a3_1/40 - 0.153233)
(15*a1_1)/16 + a2_1/4 + a3_1/32 - sin((17*a1_1)/60 + a2_1/8 + a3_1/40 - 0.0282326)
(3*a1_1)/4 + a2_1/2 + a3_1/8 - sin((17*a1_1)/60 + a2_1/8 + a3_1/40 + 0.846767)
as you can see each component is a non-linear equation. The three component of the vector forms a system of three non-linear equations having it's variables named as a1_1, a1_2and a1_3. I want to solve this system by fsolve.
How do I do that for arbitrary n?
To use fsolve, your function must accept a vector input and return a vector of the same size. In your case you can accomplish this with an anonymous function:
f = #(a)[a(1) - sin(17*a(1)/60 + a(2)/8 + a(3)/40 - 0.153233);...
15*a(1)/16 + a(2)/4 + a(3)/32 - sin(17*a(1)/60 + a(2)/8 + a(3)/40 - 0.0282326);...
3*a(1)/4 + a(2)/2 + a(3)/8 - sin(17*a(1)/60 + a(2)/8 + a(3)/40 + 0.846767)];
n = 3;
a0 = zeros(n,1); % Initial guess
opts = optimoptions('fsolve','Display','iter','TolFun',1e-8);
[a_sol,a_val,exitflag] = fsolve(f,a0,opts)
This returns
a_sol =
-0.002818738864459
-0.687953796565011
9.488284986072076
Of course there may be more than one solution, especially for larger n. You can choose your initial guess to find the others. See the documentation for fsolve and optimoptions for further details on on specifying options.
Did you try using the solve command ?
[y1,...,yN] = solve(eqns,vars) solves the system of equations eqns for the variables vars. The solutions are assigned to the variables y1,...,yN. If you do not specify the variables, solve uses symvar to find the variables to solve for. In this case, the number of variables that symvar finds is equal to the number of output arguments N.

Use of (ilaplace) give different result through sym variable and directly

I have a symbolic variable, which contain, for example:
p =
(9311.0*s + 6.12e9)/(s^2 + 8500.0*s + 3.61e11)
where s - also symbolic.
Then, if I use inverse laplace through variable p, then result is
>>result=vpa(ilaplace(p,s,n),3)
result =
exp(n*(- 4255.0 + 6.01e5*i))*(4666.0 - 5066.0*i) + exp(n*(- 4255.0 - 6.01e5*i))*(4666.0 + 5066.0*i)
But if I put expression directly, I will get what I expect (by formula in Korn's book or by definition):
vpa(ilaplace((9311.0*s + 6.12e9)/(s^2 + 8500.0*s + 3.61e11),s,n),3)
ans =
9311.0*exp(-4255.0*n)*(cos(6.01e5*n) + 1.09*sin(6.01e5*n))
Why? And what should I do to get right answer through variable?
P.S. vpa - is not influenced on main goal. It only left 3 digits in this case after point.
P.S.S. Added more code:
t = tf(linsys1) %linsys1 - from simulink circuit
%get coefficients from transfer function t
[num,den] = tfdata(t);
syms s n real
% convert transfer function to symbolic
t_sym = poly2sym(cell2mat(num),s)/poly2sym(cell2mat(den),s);
functionInMuPad=['partfrac(',char(t_sym),',s,Domain = R_)']; %collect expression in string format
simpleFraction=evalin(symengine,functionInMuPad); % sum of simple fractions (only MuPad allows get denominator of 2nd order)
functionInMuPad2=['op(',char(simpleFraction),')']; %collect expression in string format
vectorOfOperand=evalin(symengine,functionInMuPad2); % vector of simple fractions
for k=1:length(vectorOfOperand)-1
z(k,1)=ilaplace(vectorOfOperand(k),s,n);
end
So, something wrong with vectorOfOperand.
ilaplace(vectorOfOperand(1)) gives complex result, but if copy (ctrl+c) value of vectorOfOperand(1) and make newVariable=Ctrl+V, then ilaplace(newVariable) - it's ok either in command window or in m-file:
bbb =(9313.8202564498255348020392071286*s + 6122529964.4040716985063406588769)/(8500.4056471180697831891961467533*s + s^2 + 360665607284.96754103451618653904);
ilaplace(bbb,s,n)
ans=9311.0*exp(-4255.0*n)*(cos(6.01e5*n) + 1.09*sin(6.01e5*n)) %after vpa
Kind of magic anyway. vectorOfOperand - is sym. I even made this:
vectorOfOperand=char(vectorOfOperand);
vectorOfOperand=sym(vectorOfOperand); it doesn't help..

parametric method Autoregression moving average(ARMA) to AR process

i want to know how to convert ARMA (Autoregression moving average) process to AR(Autoregression) process by PARAMETRIC METHOD.
i.e. i have a transfer function H(z) = (a + b*z)/(c +d*z) e.g. H(z) = (0.26 + 0.073*z^-1)/(1 - z^-1) i.e. Autoregression(ARMA)
and i want to convert it to AR process i.e. H(z) = 1/(p + q*z + r*z^2 + ...)(i.e only pole system).
Kindly Give some hints.
Thanks in advance!
if you are looking for partial partial fraction expansion commands, I suggest you try
[r, p, k] = residue(b,a)
http://www.mathworks.com/help/matlab/ref/residue.html

Use value in previous cell of matlab matrix for use in equation

Hi I am working with the following code (Brute force method).
"PV_supply" and "WT_supply" and "Demand" are 48x1 size.
what I am trying to do is calculate the "Energy_battery" value for each of the 48 rows. However to do this I need to use the value of "Energy_battery" from the previous row in the calculations of each row which I haven't figured out how to code and was hoping for some help on this. therefore the equation for "Energy_battery" in row 1 uses the "Energy_battery" value in row 1 for the equation etc
My code is:
for number_panels = 0:5
for number_turbines = 0:3
for h=1:24 %# hours
for d = 1:number_of_days %# which day
n = h + 24*(d-1);
Energy_battery(number_panels + 1, number_turbines + 1,1,1) = 100;
Energy_battery(number_panels + 1, number_turbines + 1,n+1,1) =...
Energy_battery(number_panels + 1, number_turbines + 1,h,1) + ...
((PV_supply(n)*number_panels + WT_supply(n)*number_turbines) - ...
Demand(n)/inverter_efficiency)*battery_charging_efficiency;
This is an extended comment, only an answer to parts of your question, though I think it has relevance to your other recent questions too.
It's helpful to think of Matlab as an array processing language, the natural 'unit' of computation is an array rather than a scalar as in many other languages. If you find yourself writing loops to iterate over the elements of an array stop and think, there is likely to be a more 'natural' way of expressing the same calculation without the loops. There's nothing absolutely wrong with loops but over-reliance on them can have 2 deleterious effects:
Matlab code without loops is often (much) faster than the equivalent with loops;
Code with loops is often more cluttered, and requires more variables to be used, than the equivalent without loops. The clutter is a hindrance when it comes to understanding the code
So, for example, your statement
Energy_battery(number_panels + 1, number_turbines + 1,1,1) = 100;
could be lifted out of your loop nest entirely and rewritten as
Energy_battery(:,:,1,1) = 100;
Now, for the main bulk of your code, if I've understood it correctly, you want to update each element at Energy_battery(:,:,n+1,1) based on the values in Energy_battery(:,:,n,1) and in element n of the other vectors you have. First, let's tidy this up
((PV_supply(n)*number_panels + WT_supply(n)*number_turbines) - Demand(n)/inverter_efficiency)*battery_charging_efficiency
could be rewritten as
((PV_supply*number_panels + WT_supply*number_turbines) - Demand/inverter_efficiency)*battery_charging_efficiency
for Matlab multiplying an array by a scalar applies the multiplication to each element of the array. And again, this doesn't need a loop over the values of an index such as n.
I'm afraid I have to go and do some work now, will come back later and finish the lesson if no-one else does. Feel free to edit this answer if you want to.