Asked by Daniel Harper
on 18 Feb 2019

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?

Answer by Star Strider
on 18 Feb 2019

Accepted Answer

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.

Star Strider
on 20 Feb 2019

Maximize it with respect to what?

The ‘y’ variable is a (1 x 12) vector that is not itself a function of any other variables. What do you intend by ‘maximizing’ it? You can get the largest value (and its index) with the max (link) function.

Star Strider
on 20 Feb 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
on 20 Feb 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.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## madhan ravi (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/445608-can-t-run-an-equation-in-a-function-that-works-in-a-script#comment_672521

## Daniel Harper (view profile)

## Direct link to this comment

https://ch.mathworks.com/matlabcentral/answers/445608-can-t-run-an-equation-in-a-function-that-works-in-a-script#comment_672543

Sign in to comment.