Optimazition MFs of Fuzzy inference by genetic algorithm - matlab
I am using GA to optimize the parameters of the membership functions in my fuzzy system.
I create a function for fitness:
function y = gafuzzy(x)
global FISsys
global allData
global realResult
FISsys = readfis('aCAess.fis');
allData = importdata('ab.mat');
realResult = importdata('ad.mat');
FISsys.input(1,1).mf(1,1).params = [x(1) x(2) x(3)];
FISsys.input(1,1).mf(1,2).params = [x(4) x(5) x(6)];
FISsys.input(1,2).mf(1,1).params = [x(7) x(8) x(9)];
FISsys.input(1,2).mf(1,2).params = [x(10) x(11) x(12)];
FISsys.output.mf(1,1).params = [x(13) x(14) x(15)];
FISsys.output.mf(1,2).params = [x(16) x(17) x(18)];
c = evalfis(allData,FISsys);
e=sum(abs(c-realResult));
y = e;
end
And A[15*18] matrix for linear inequalities is :
A = [1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,1,-1,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,1,-1,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,1,-1,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,1,-1,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,1,-1,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,1,-1,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,1,-1,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,-1;
0,1,0,0,-1,0,0,0,0,0,0,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,1,0,0,-1,0,0,0,0,0,0,0;
0,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,-1,0]
and b[15*1] vector is:
b = [0;0;0;0;0;0;0;0;0;0;0;0;0;0;0]
but when I run GA, I get this error:
Illegal parameters in fisTriangleMf() --> a > b
why?
Generally, in the triangle MF the first number, here a (shows the left vertex) should be smaller than the second number, here b (the top vertex). So you can have a triangle MF like [-1 0 1] but it cannot be like [0 -1 1].
in your code, I assume sometimes you don't satisfy the inequality in one of those places:
[x(1) < x(2) < x(3)];
[x(4) < x(5) < x(6)];
[x(7) < x(8) < x(9)];
[x(10) < x(11) < x(12)];
....
if the program is randomizing these values, you can bound them in your code easily by checking and replacing, for instance:
if x(1) >= x(2)
tmp = x(1);
x(1) = x(2);
x(2) = tmp;
end
Related
Finite Element assembly
I'm having serious problems in a simple example of fem assembly. I just want to assemble the Mass matrix without any coefficient. The geometry is simple: conn=[1, 2, 3]; p = [0 0; 1 0; 0 1]; I made it like this so that the physical element will be equal to the reference one. my basis functions: phi_1 = #(eta) 1 - eta(1) - eta(2); phi_2 = #(eta) eta(1); phi_3 = #(eta) eta(2); phi = {phi_1, phi_2, phi_3}; Jacobian matrix: J = #(x,y) [x(2) - x(1), x(3) - x(1); y(2) - y(1), y(3) - y(1)]; The rest of the code: M = zeros(np,np); for K = 1:size(conn,1) l2g = conn(K,:); %local to global mapping x = p(l2g,1); %node x-coordinate y = p(l2g,2); %node y-coordinate jac = J(x,y); loc_M = localM(jac, phi); M(l2g, l2g) = M(l2g, l2g) + loc_M; %add element masses to M end localM: function loc_M = localM(J,phi) d_J = det(J); loc_M = zeros(3,3); for i = 1:3 for j = 1:3 loc_M(i,j) = d_J * quadrature(phi{i}, phi{j}); end end end quadrature: function value = quadrature(phi_i, phi_j) p = [1/3, 1/3; 0.6, 0.2; 0.2, 0.6; 0.2, 0.2]; w = [-27/96, 25/96, 25/96, 25/96]; res = 0; for i = 1:size(p,1) res = res + phi_i(p(i,:)) * phi_j(p(i,:)) * w(i); end value = res; end For the simple entry (1,1) I obtain 0.833, while computing the integral by hand or on wolfram alpha I get 0.166 (2 times the result of the quadrature). I tried with different points and weights for quadrature, but really I do not know what I am doing wrong.
Solving System of Second Order Ordinary Differential Equation in Matlab
Introduction I am using Matlab to simulate some dynamic systems through numerically solving systems of Second Order Ordinary Differential Equations using ODE45. I found a great tutorial from Mathworks (link for tutorial at end) on how to do this. In the tutorial the system of equations is explicit in x and y as shown below: x''=-D(y) * x' * sqrt(x'^2 + y'^2) y''=-D(y) * y' * sqrt(x'^2 + y'^2) + g(y) Both equations above have form y'' = f(x, x', y, y') Question However, I am coming across systems of equations where the variables can not be solved for explicitly as shown in the example. For example one of the systems has the following set of 3 second order ordinary differential equations: y double prime equation y'' - .5*L*(x''*sin(x) + x'^2*cos(x) + (k/m)*y - g = 0 x double prime equation .33*L^2*x'' - .5*L*y''sin(x) - .33*L^2*C*cos(x) + .5*g*L*sin(x) = 0 A single prime is first derivative A double prime is second derivative L, g, m, k, and C are given parameters. How can Matlab be used to numerically solve a set of second order ordinary differential equations where second order can not be explicitly solved for? Thanks!
Your second system has the form a11*x'' + a12*y'' = f1(x,y,x',y') a21*x'' + a22*y'' = f2(x,y,x',y') which you can solve as a linear system [x'', y''] = A\f or in this case explicitly using Cramer's rule x'' = ( a22*f1 - a12*f2 ) / (a11*a22 - a12*a21) y'' accordingly. I would strongly recommend leaving the intermediate variables in the code to reduce chances for typing errors and avoid multiple computation of the same expressions. Code could look like this (untested) function dz = odefunc(t,z) x=z(1); dx=z(2); y=z(3); dy=z(4); A = [ [-.5*L*sin(x), 1] ; [.33*L^2, -0.5*L*sin(x)] ] b = [ [dx^2*cos(x) + (k/m)*y-g]; [-.33*L^2*C*cos(x) + .5*g*L*sin(x)] ] d2 = A\b dz = [ dx, d2(1), dy, d2(2) ] end
Yes your method is correct! I post the following code below: %Rotating Pendulum Sym Main clc clear all; %Define parameters global M K L g C; M = 1; K = 25.6; L = 1; C = 1; g = 9.8; % define initial values for theta, thetad, del, deld e_0 = 1; ed_0 = 0; theta_0 = 0; thetad_0 = .5; initialValues = [e_0, ed_0, theta_0, thetad_0]; % Set a timespan t_initial = 0; t_final = 36; dt = .01; N = (t_final - t_initial)/dt; timeSpan = linspace(t_final, t_initial, N); % Run ode45 to get z (theta, thetad, del, deld) [t, z] = ode45(#RotSpngHndl, timeSpan, initialValues); %initialize variables e = zeros(N,1); ed = zeros(N,1); theta = zeros(N,1); thetad = zeros(N,1); T = zeros(N,1); V = zeros(N,1); x = zeros(N,1); y = zeros(N,1); for i = 1:N e(i) = z(i, 1); ed(i) = z(i, 2); theta(i) = z(i, 3); thetad(i) = z(i, 4); T(i) = .5*M*(ed(i)^2 + (1/3)*L^2*C*sin(theta(i)) + (1/3)*L^2*thetad(i)^2 - L*ed(i)*thetad(i)*sin(theta(i))); V(i) = -M*g*(e(i) + .5*L*cos(theta(i))); E(i) = T(i) + V(i); end figure(1) plot(t, T,'r'); hold on; plot(t, V,'b'); plot(t,E,'y'); title('Energy'); xlabel('time(sec)'); legend('Kinetic Energy', 'Potential Energy', 'Total Energy'); Here is function handle file for ode45: function dz = RotSpngHndl(~, z) % Define Global Parameters global M K L g C A = [1, -.5*L*sin(z(3)); -.5*L*sin(z(3)), (1/3)*L^2]; b = [.5*L*z(4)^2*cos(z(3)) - (K/M)*z(1) + g; (1/3)*L^2*C*cos(z(3)) + .5*g*L*sin(z(3))]; X = A\b; % return column vector [ed; edd; ed; edd] dz = [z(2); X(1); z(4); X(2)];
How to iterate over functions?
I would like to apply loop over a function. I have the following "mother" code: v = 1; fun = #root; x0 = [0,0] options = optimset('MaxFunEvals',100000,'MaxIter', 10000 ); x = fsolve(fun,x0, options) In addition, I have the following function in a separate file: function D = root(x) v = 1; D(1) = x(1) + x(2) + v - 2; D(2) = x(1) - x(2) + v - 1.8; end Now, I would like to find roots when v = sort(rand(1,1000)). In other words, I would like to iterate over function for each values of v.
You will need to modify root to accept an additional variable (v) and then change the function handle to root to an anonymous function which feeds in the v that you want function D = root(x, v) D(1) = x(1) + x(2) + v - 2; D(2) = x(1) - x(2) + v - 1.8; end % Creates a function handle to root using a specific value of v fun = #(x)root(x, v(k))
Just in case that equation is your actual equation (and not a dummy example): that equation is linear, meaning, you can solve it for all v with a simple mldivide: v = sort(rand(1,1000)); x = [1 1; 1 -1] \ bsxfun(#plus, -v, [2; 1.8]) And, in case those are not your actual equations, you don't need to loop, you can vectorize the whole thing: function x = solver() options = optimset('Display' , 'off',... 'MaxFunEvals', 1e5,... 'MaxIter' , 1e4); v = sort(rand(1, 1000)); x0 = repmat([0 0], numel(v), 1); x = fsolve(#(x)root(x,v'), x0, options); end function D = root(x,v) D = [x(:,1) + x(:,2) + v - 2 x(:,1) - x(:,2) + v - 1.8]; end This may or may not be faster than looping, it depends on your actual equations. It may be slower because fsolve will need to compute a Jacobian of 2000×2000 (4M elements), instead of 2×2, 1000 times (4k elements). But, it may be faster because the startup cost of fsolve can be large, meaning, the overhead of many calls may in fact outweigh the cost of computing the larger Jacobian. In any case, providing the Jacobian as a second output will speed everything up rather enormously: function solver() options = optimset('Display' , 'off',... 'MaxFunEvals', 1e5,... 'MaxIter' , 1e4,... 'Jacobian' , 'on'); v = sort(rand(1, 1000)); x0 = repmat([1 1], numel(v), 1); x = fsolve(#(x)root(x,v'), x0, options); end function [D, J] = root(x,v) % Jacobian is constant: persistent J_out if isempty(J_out) one = speye(numel(v)); J_out = [+one +one +one -one]; end % Function values at x D = [x(:,1) + x(:,2) + v - 2 x(:,1) - x(:,2) + v - 1.8]; % Jacobian at x: J = J_out; end
vvec = sort(rand(1,2)); x0 = [0,0]; for v = vvec, fun = #(x) root(v, x); options = optimset('MaxFunEvals',100000,'MaxIter', 10000 ); x = fsolve(fun, x0, options); end with function definition: function D = root(v, x) D(1) = x(1) + x(2) + v - 2; D(2) = x(1) - x(2) + v - 1.8; end
Genetic Algorithm Constraints
How can I pass the following constraints to the Matlab ga optimization function? Note that x is a vector 1xnvars Constraint 1 0.2 <= sum(x,2)/(W*H) <= 0.4 where `W` and `H` are two constant. Constraint 2 x(1) >= x(2) >= ... >= x(size(x,1))
Refer here for the documentation. A = cat(1,ones(1,nvars), ones(1,nvars)*-1)/W/H; b = [0.4;-0.2]; function [ceq ce] = noncolon1(x) ce = []; ceq = x(1,2:end) - x(1,1:end-1); end [x , fval] = ga(#fitnessfunc,nvars,A,b,[],[],[],[],#noncolon1);
Cubic Spline Program
I'm trying to write a cubic spline interpolation program. I have written the program but, the graph is not coming out correctly. The spline uses natural boundary conditions(second dervative at start/end node are 0). The code is in Matlab and is shown below, clear all %Function to Interpolate k = 10; %Number of Support Nodes-1 xs(1) = -1; for j = 1:k xs(j+1) = -1 +2*j/k; %Support Nodes(Equidistant) end; fs = 1./(25.*xs.^2+1); %Support Ordinates x = [-0.99:2/(2*k):0.99]; %Places to Evaluate Function fx = 1./(25.*x.^2+1); %Function Evaluated at x %Cubic Spline Code(Coefficients to Calculate 2nd Derivatives) f(1) = 2*(xs(3)-xs(1)); g(1) = xs(3)-xs(2); r(1) = (6/(xs(3)-xs(2)))*(fs(3)-fs(2)) + (6/(xs(2)-xs(1)))*(fs(1)-fs(2)); e(1) = 0; for i = 2:k-2 e(i) = xs(i+1)-xs(i); f(i) = 2*(xs(i+2)-xs(i)); g(i) = xs(i+2)-xs(i+1); r(i) = (6/(xs(i+2)-xs(i+1)))*(fs(i+2)-fs(i+1)) + ... (6/(xs(i+1)-xs(i)))*(fs(i)-fs(i+1)); end e(k-1) = xs(k)-xs(k-1); f(k-1) = 2*(xs(k+1)-xs(k-1)); r(k-1) = (6/(xs(k+1)-xs(k)))*(fs(k+1)-fs(k)) + ... (6/(xs(k)-xs(k-1)))*(fs(k-1)-fs(k)); %Tridiagonal System i = 1; A = zeros(k-1,k-1); while i < size(A)+1; A(i,i) = f(i); if i < size(A); A(i,i+1) = g(i); A(i+1,i) = e(i); end i = i+1; end for i = 2:k-1 %Decomposition e(i) = e(i)/f(i-1); f(i) = f(i)-e(i)*g(i-1); end for i = 2:k-1 %Forward Substitution r(i) = r(i)-e(i)*r(i-1); end xn(k-1)= r(k-1)/f(k-1); for i = k-2:-1:1 %Back Substitution xn(i) = (r(i)-g(i)*xn(i+1))/f(i); end %Interpolation if (max(xs) <= max(x)) error('Outside Range'); end if (min(xs) >= min(x)) error('Outside Range'); end P = zeros(size(length(x),length(x))); i = 1; for Counter = 1:length(x) for j = 1:k-1 a(j) = x(Counter)- xs(j); end i = find(a == min(a(a>=0))); if i == 1 c1 = 0; c2 = xn(1)/6/(xs(2)-xs(1)); c3 = fs(1)/(xs(2)-xs(1)); c4 = fs(2)/(xs(2)-xs(1))-xn(1)*(xs(2)-xs(1))/6; t1 = c1*(xs(2)-x(Counter))^3; t2 = c2*(x(Counter)-xs(1))^3; t3 = c3*(xs(2)-x(Counter)); t4 = c4*(x(Counter)-xs(1)); P(Counter) = t1 +t2 +t3 +t4; else if i < k-1 c1 = xn(i-1+1)/6/(xs(i+1)-xs(i-1+1)); c2 = xn(i+1)/6/(xs(i+1)-xs(i-1+1)); c3 = fs(i-1+1)/(xs(i+1)-xs(i-1+1))-xn(i-1+1)*(xs(i+1)-xs(i-1+1))/6; c4 = fs(i+1)/(xs(i+1)-xs(i-1+1))-xn(i+1)*(xs(i+1)-xs(i-1+1))/6; t1 = c1*(xs(i+1)-x(Counter))^3; t2 = c2*(x(Counter)-xs(i-1+1))^3; t3 = c3*(xs(i+1)-x(Counter)); t4 = c4*(x(Counter)-xs(i-1+1)); P(Counter) = t1 +t2 +t3 +t4; else c1 = xn(i-1+1)/6/(xs(i+1)-xs(i-1+1)); c2 = 0; c3 = fs(i-1+1)/(xs(i+1)-xs(i-1+1))-xn(i-1+1)*(xs(i+1)-xs(i-1+1))/6; c4 = fs(i+1)/(xs(i+1)-xs(i-1+1)); t1 = c1*(xs(i+1)-x(Counter))^3; t2 = c2*(x(Counter)-xs(i-1+1))^3; t3 = c3*(xs(i+1)-x(Counter)); t4 = c4*(x(Counter)-xs(i-1+1)); P(Counter) = t1 +t2 +t3 +t4; end end end P = P'; P(length(x)) = NaN; plot(x,P,x,fx) When I run the code, the interpolation function is not symmetric and, it doesn't converge correctly. Can anyone offer any suggestions about problems in my code? Thanks.
I wrote a cubic spline package in Mathematica a long time ago. Here is my translation of that package into Matlab. Note I haven't looked at cubic splines in about 7 years, so I'm basing this off my own documentation. You should check everything I say. The basic problem is we are given n data points (x(1), y(1)) , ... , (x(n), y(n)) and we wish to calculate a piecewise cubic interpolant. The interpolant is defined as S(x) = { Sk(x) when x(k) <= x <= x(k+1) { 0 otherwise Here Sk(x) is a cubic polynomial of the form Sk(x) = sk0 + sk1*(x-x(k)) + sk2*(x-x(k))^2 + sk3*(x-x(k))^3 The properties of the spline are: The spline pass through the data point Sk(x(k)) = y(k) The spline is continuous at the end-points and thus continuous everywhere in the interpolation interval Sk(x(k+1)) = Sk+1(x(k+1)) The spline has continuous first derivative Sk'(x(k+1)) = Sk+1'(x(k+1)) The spline has continuous second derivative Sk''(x(k+1)) = Sk+1''(x(k+1)) To construct a cubic spline from a set of data point we need to solve for the coefficients sk0, sk1, sk2 and sk3 for each of the n-1 cubic polynomials. That is a total of 4*(n-1) = 4*n - 4 unknowns. Property 1 supplies n constraints, and properties 2,3,4 each supply an additional n-2 constraints. Thus we have n + 3*(n-2) = 4*n - 6 constraints and 4*n - 4 unknowns. This leaves two degrees of freedom. We fix these degrees of freedom by setting the second derivative equal to zero at the start and end nodes. Let m(k) = Sk''(x(k)) , h(k) = x(k+1) - x(k) and d(k) = (y(k+1) - y(k))/h(k). The following three-term recurrence relation holds h(k-1)*m(k-1) + 2*(h(k-1) + h(k))*m(k) + h(k)*m(k+1) = 6*(d(k) - d(k-1)) The m(k) are unknowns we wish to solve for. The h(k) and d(k) are defined by the input data. This three-term recurrence relation defines a tridiagonal linear system. Once the m(k) are determined the coefficients for Sk are given by sk0 = y(k) sk1 = d(k) - h(k)*(2*m(k) + m(k-1))/6 sk2 = m(k)/2 sk3 = m(k+1) - m(k)/(6*h(k)) Okay that is all the math you need to know to completely define the algorithm to compute a cubic spline. Here it is in Matlab: function [s0,s1,s2,s3]=cubic_spline(x,y) if any(size(x) ~= size(y)) || size(x,2) ~= 1 error('inputs x and y must be column vectors of equal length'); end n = length(x) h = x(2:n) - x(1:n-1); d = (y(2:n) - y(1:n-1))./h; lower = h(1:end-1); main = 2*(h(1:end-1) + h(2:end)); upper = h(2:end); T = spdiags([lower main upper], [-1 0 1], n-2, n-2); rhs = 6*(d(2:end)-d(1:end-1)); m = T\rhs; % Use natural boundary conditions where second derivative % is zero at the endpoints m = [ 0; m; 0]; s0 = y; s1 = d - h.*(2*m(1:end-1) + m(2:end))/6; s2 = m/2; s3 =(m(2:end)-m(1:end-1))./(6*h); Here is some code to plot a cubic spline: function plot_cubic_spline(x,s0,s1,s2,s3) n = length(x); inner_points = 20; for i=1:n-1 xx = linspace(x(i),x(i+1),inner_points); xi = repmat(x(i),1,inner_points); yy = s0(i) + s1(i)*(xx-xi) + ... s2(i)*(xx-xi).^2 + s3(i)*(xx - xi).^3; plot(xx,yy,'b') plot(x(i),0,'r'); end Here is a function that constructs a cubic spline and plots in on the famous Runge function: function cubic_driver(num_points) runge = #(x) 1./(1+ 25*x.^2); x = linspace(-1,1,num_points); y = runge(x); [s0,s1,s2,s3] = cubic_spline(x',y'); plot_points = 1000; xx = linspace(-1,1,plot_points); yy = runge(xx); plot(xx,yy,'g'); hold on; plot_cubic_spline(x,s0,s1,s2,s3); You can see it in action by running the following at the Matlab prompt >> cubic_driver(5) >> clf >> cubic_driver(10) >> clf >> cubic_driver(20) By the time you have twenty nodes your interpolant is visually indistinguishable from the Runge function. Some comments on the Matlab code: I don't use any for or while loops. I am able to vectorize all operations. I quickly form the sparse tridiagonal matrix with spdiags. I solve it using the backslash operator. I counting on Tim Davis's UMFPACK to handle the decomposition and forward and backward solves. Hope that helps. The code is available as a gist on github https://gist.github.com/1269709
There was a bug in spline function, generated (n-2) by (n-2) should be symmetric: lower = h(2:end); main = 2*(h(1:end-1) + h(2:end)); upper = h(1:end-1); http://www.mpi-hd.mpg.de/astrophysik/HEA/internal/Numerical_Recipes/f3-3.pdf