From: Etienne on
Hi everybody,

I'm using the genetic algorithm toolbox and, as needed, the fitness function is in a matlab function.

The problem is: in the fitness function resolution there is a simulink model which is using variable coming from the matlab function. Simulink return the following error:

Variable x is not defined
Variable y is not defined...

Hypothesis: the variable defined in the matlab function are not in the "main workspace", so simulink can't read them.

Simple exemple: trying to minimise the displacement
In that case, simulink return: variable "mass" is not defined, variable "a" is not defined...

%Main program
[x,fval]=ga(fitnessfunc,2)

%Matlab function
function y=fitnessfunc(w)
a=w(1);
b=w(2);
mass=400:
sim(modelisation.mdl,10) %simulation using "a","b" and "mass" variables and saving in the workspace an "displacement" array.

y=max(displacement)
end %end of the function

thanks
Etienne
From: James Allison on
You need to use the simset command before sim to set the workspace to
the fitness function workspace:

options = simset('DstWorkspace','current','SrcWorkspace','current');

I hope this helps.

-James

Etienne wrote:
> Hi everybody,
>
> I'm using the genetic algorithm toolbox and, as needed, the fitness
> function is in a matlab function.
>
> The problem is: in the fitness function resolution there is a simulink
> model which is using variable coming from the matlab function. Simulink
> return the following error:
>
> Variable x is not defined
> Variable y is not defined...
>
> Hypothesis: the variable defined in the matlab function are not in the
> "main workspace", so simulink can't read them.
>
> Simple exemple: trying to minimise the displacement
> In that case, simulink return: variable "mass" is not defined, variable
> "a" is not defined...
>
> %Main program
> [x,fval]=ga(fitnessfunc,2)
>
> %Matlab function
> function y=fitnessfunc(w)
> a=w(1);
> b=w(2);
> mass=400:
> sim(modelisation.mdl,10) %simulation using "a","b" and "mass" variables
> and saving in the workspace an "displacement" array.
>
> y=max(displacement)
> end %end of the function
>
> thanks Etienne
 | 
Pages: 1
Prev: dpsk simulink model
Next: solve Ax=b