Can't run an equation in a function that works in a script

1 visualización (últimos 30 días)
Hi,
I've used the following code for an equation in MATLAB and it works perfectly well in a script (cp1 and u1 have already been defined):
syms u4
eqn3 = cp1 == (u1+u4)*(u1.^2-u4.^2)/(2*u1.^3);
U4= double(solve(eqn3,u4));
u4 = real(U4( U4 > 0 ));
However, when I've tried to implement it into a function it doesn't work.
Can anyone help me with this please?
  2 comentarios
madhan ravi
madhan ravi el 18 de Feb. de 2019
show us your function
Daniel Harper
Daniel Harper el 18 de Feb. de 2019
function [y] = test(x)
c1 = 0.5176;
c2 = 116;
c3 = 0.4;
c4 = 5;
c5 = 21;
c6 = 0.0068;
% equation 1 lamdai1
z1 = 1/(x(2) + 0.08*x(1)) - 0.035/((x(1).^3) +1);
y(1)=1/z1;
%equation 2 cp1
y(2) = c1*((c2/y(1))-c3*x(1)-c4)*exp(-c5/y(1))+c6*x(2);
%equation 3 u4 (z3)
syms y(3)
eqn3 = y(2) == (u1+y(3))*(u1.^2-y(3).^2)/(2*u1.^3);
Z3 = double(solve(eqn3,y(3)));
z3 = real(Z3( Z3>0 ));
y(3)=z3
%equation 4
y(4) = 0.5*(u1+y(3));
end
I'm using a genetic algorithm for inputs x(1) and x(2). cp1 and u4 etc. have been equated to y(1) etc.

Iniciar sesión para comentar.

Respuesta aceptada

Star Strider
Star Strider el 18 de Feb. de 2019
Try this:
function [y] = test(x)
c1 = 0.5176;
c2 = 116;
c3 = 0.4;
c4 = 5;
c5 = 21;
c6 = 0.0068;
% equation 1 lamdai1
z1 = 1/(x(2) + 0.08*x(1)) - 0.035/((x(1).^3) +1);
y = sym('y', [1 4])
y(1)=1/z1;
%equation 2 cp1
y(2) = c1*((c2/y(1))-c3*x(1)-c4)*exp(-c5/y(1))+c6*x(2);
%equation 3 u4 (z3)
u1 = 3; % <— PROVIDE CORRECT VALUE
eqn3 = y(2) - ((u1+y(3))*(u1.^2-y(3).^2)/(2*u1.^3));
eqn3 = vpa(eqn3);
U4p = sym2poly(eqn3);
Z3 = roots(U4p);
z3 = real(Z3( Z3>0 ));
y(3)=z3;
%equation 4
y(4) = vpa(0.5*(u1+y(3)));
y = double(y);
end
It runs without error for me. You must determine if it produces the correct result.
Note that this cannot be your fitness function, since a fitness function is required to produce a scalar output.
  8 comentarios
Star Strider
Star Strider el 20 de Feb. de 2019
Daniel Harper’s ‘Answer’ moved here:
Maximize it in terms of the inputs x. Like how do I maximize the fitness fuction rather than minimize it? I've seen you can do y = -x for the fuction however that doesn't seem to work
Star Strider
Star Strider el 20 de Feb. de 2019
If ‘test.m’ is your entire fitness function, it is not going to work. Fitness functions require scalar outputs (at least in my experience), and ‘y’ is a (1 x 12) vector.
Negating the fitness function output is the generally accepted way to maximimize a fitness (or other objective) function. That obviously works if there is a distinct (and preferably bounded) maximum. If your fitness function is for example a function that increases without bound in any direction, there is not going to be a distinct maximum.
So first, plot the function you want to optimize, if possible. (It initially appeared to be a function of two parameters, so that could work.) That will give you some idea of what it’s doing, and if finding a distinct maximum is even possible.
Meanwhile, I have gotten yuouir function to run, and run relatively efficiently. That was the initial objective.

Iniciar sesión para comentar.

Más respuestas (0)

Community Treasure Hunt

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

Start Hunting!

Translated by