Asked by Harvey Rael
on 18 May 2018

Keep getting the message "undefined function or variable"..I'm trying to create this function to pass to a genetic algorithm to optimise for three variables (k1,k2,k3) but even when I test it I get an error. It saves as a .m file though. What's going wrong?

function y = newFitness(k) % Define constants AU = astroConstants(2); mu = astroConstants(4); % Define start and finish parameters for the exponential sinusoid. r1 = AU; % Initial radius psi = pi/2; % Final polar angle of Mars/finish transfer phi = pi/2; theta_dot = sqrt( mu/(((r1/exp(k(1)*sin(phi)))*exp((k(1)*sin(k(2)*(psi + 2*pi*k(3))+phi))))^3) * 1/(tan(atan(k(1)*k(2)*cos(k(2)*(psi + 2*pi*k(3))+phi)))^2 + k(1)*k(2)^2*sin(k(2)*(psi + 2*pi*k(3))+phi) +1) ); y = theta_dot - 94608000;

**Edit**: Following on from one of the suggestions below, there arises the following information : The function can be found apparently.

which newFitness Y:\MATLAB\newFitness.m

So here is the genetic algorithm code that I am trying to pass it on to. I'm not sure why that is important, because before I even touch the GA code, I try and test my function by typing :

newFitness([1 2 3])

Which still gives me an error message of "Undefined function or variable 'newFitness'". But here's the GA code anyways.

clc;clear; objective_function = @newFitness; nvars = 3; [k,fval]=ga(objective_function,nvars);

Answer by Harvey Rael
on 18 May 2018

Accepted Answer

***Update*** : MATLAB is a cruel mistress. I turned off my computer last night after many attempts (and after asking this question) when nothing worked. Turned it on again this morning without touching any code at all.. it works enough to give me answers. I don't know why, all paths are the same. Now I just need to provided constraints..

Thanks for all your help!

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 6 Comments

## Greg (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401332-undefined-function-or-variable-error-even-though-function-is-saved#comment_569151

Is it on your path?

## Chetna Jain (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401332-undefined-function-or-variable-error-even-though-function-is-saved#comment_569155

Kindly check if astroConstants() is defined properly and are you passing k1,k2,k3 in array?

## Jan (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401332-undefined-function-or-variable-error-even-though-function-is-saved#comment_569273

Please post the

completeerror message, such that the readers do not have to guess, which variable or function is concerned.## Harvey Rael (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401332-undefined-function-or-variable-error-even-though-function-is-saved#comment_569331

To answer your questions (and thank you for the speedy responses!).

## Geoff Hayes (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401332-undefined-function-or-variable-error-even-though-function-is-saved#comment_569338

Harvey - is the newFitness function in the MATLAB search path? In the command window, type

to see if the function can be found. If not, then add it to the search path. If it can be found, then there might be something wrong with the code that is trying to pass this function to the GA...so please copy and paste that code (which is throwing the error) to your question.

## Harvey Rael (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/401332-undefined-function-or-variable-error-even-though-function-is-saved#comment_569363

Geoff - See the edit on the question. Hope that helps.

Sign in to comment.