Code for a project, giving me an error stating unrecognized function or variable 'mass'. What is the fix?
2 views (last 30 days)
Show older comments
function design_parameters = size_auv_design(mass, length, width, height, ~, battery_capacity, communication_range)
syms mass length width height buoyancy_coefficient battery_capacity propulsion_coefficient communication_range
% Instantiate an AUV object with the user inputs
auv = AUV(mass, length, width, height, buoyancy_coefficient, battery_capacity, communication_range);
% Define the optimization problem
syms velocity positive
drag_force = auv.calculate_drag_force(velocity);
propulsion_power = calculate_propulsion_power(velocity, drag_force);
battery_life = calculate_battery_life(auv.power_consumption, battery_capacity);
buoyancy_force = calculate_buoyancy_force(auv.volume);
objective = propulsion_power + battery_life;
% Set the optimization constraints
constraints = [auv.weight == buoyancy_force, propulsion_power <= auv.max_propulsion_power];
% Solve the optimization problem
options = optimoptions('fmincon', 'Display', 'none');
solution = fmincon(objective, 0, [], [], [], [], 0, 2, constraints, options);
% Calculate the design parameters
optimal_velocity = double(solution);
auv.simulate(10); % simulate for 10 seconds
auv_drag_force = auv.calculate_drag_force(optimal_velocity);
optimal_prop_power = calculate_propulsion_power(optimal_velocity, auv_drag_force);
optimal_battery_life = calculate_battery_life(auv.power_consumption, battery_capacity);
optimal_buoyancy_force = calculate_buoyancy_force(auv.volume);
% Store the design parameters in a struct
design_parameters.optimal_velocity = optimal_velocity;
design_parameters.optimal_prop_power = optimal_prop_power;
design_parameters.optimal_battery_life = optimal_battery_life;
design_parameters.optimal_buoyancy_force = optimal_buoyancy_force;
end
% Define the supporting functions
function propulsion_power = calculate_propulsion_power(velocity, drag_force)
% This function calculates the propulsion power required for a given velocity and drag force
% Replace with your own calculation
propulsion_power = 0.1*velocity + 0.2*drag_force;
end
function battery_life = calculate_battery_life(power_consumption, battery_capacity)
% This function calculates the battery life for a given power consumption and battery capacity
% Replace with your own calculation
battery_life = battery_capacity/power_consumption;
end
function buoyancy_force = calculate_buoyancy_force(volume)
% This function calculates the buoyancy force for a given volume
% Replace with your own calculation
buoyancy_force = 9.81*volume;
end
2 Comments
Sai
on 4 Apr 2023
Can you please send me the screenshot of the error message and other part of the code where you are calling these functions?
Cris LaPierre
on 4 Apr 2023
It seems odd to me to have your function input values immediately be redefined as syms.
a=5
syms a
a
Answers (2)
Cris LaPierre
on 4 Apr 2023
The most likely reason is that you have not yet defined a variable mass.
design_parameters = size_auv_design(mass, length, width, height, battery_capacity, communication_range)
Also note that you cannot use a ~ to skip an input argument. Since you are the one defining the function, just remove it.
We would need to know what your function AUV is to say for certain, but I do not believe you should be redefining all your inputs as symbolic variables. So if I were to rewrite your code, it might look like this.
function design_parameters = size_auv_design(mass, length, width, height, battery_capacity, communication_range)
% Instantiate an AUV object with the user inputs
auv = AUV(mass, length, width, height, buoyancy_coefficient, battery_capacity, communication_range);
...
end
When you call your function you need to pass in values for each input.
mass = 50;
length = 5;
width = 3;
height = 2;
battery_capacity = 100;
communication_range = 75;
myParams = size_auv_design(mass, length, width, height, battery_capacity, communication_range)
0 Comments
Steven Lord
on 4 Apr 2023
So your function accepts seven input arguments from the user:
function design_parameters = size_auv_design(mass, length, width, height, ~, battery_capacity, communication_range)
but then immediately throws them away and creates symbolic variables with those same names? [By the way some of your variable names like length, width, and height already have meaning in MATLAB. Consider renaming them; while MATLAB sees those variables as existing in your function you will not be able to call the functions of the same name.]
syms mass length width height buoyancy_coefficient battery_capacity propulsion_coefficient communication_range
If you expected this code to convert the values the user passed into your function when they called it, use sym instead.
mass = sym(mass);
auvLength = sym(auvLength); % renamed from length to auvLength
% etc
Note that this assumes that when you called size_auv_design you called it with 7 inputs. If you just ran:
design_parameters = size_auv_design;
MATLAB doesn't define the mass variable in the function call's workspace so you can't use it later on.
Skipping ahead a bit:
objective = propulsion_power + battery_life;
I think this is a symbolic expression, but I'm not certain fmincon accepts symbolic expressions as the objective function directly. You'd need to convert it into a function handle with matlabFunction in order to use it as the objective function.
1 Comment
Cris LaPierre
on 4 Apr 2023
Just adding that, if you do in fact want to use symbolic variables, you will have to change the names of length width and height. MATLAB will not let you create symbolic variables with those names inside a function.
test
function test()
syms length
end
See Also
Categories
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!