Please help for objectiveFunction command.
1 次查看(过去 30 天)
显示 更早的评论
% Optimization of Spring Constants and Damping Coefficients for Minimum Acceleration
% Given upper and lower boundaries for spring constants and damping coefficients
lower_bound = [1000, 1000, 50, 50];
upper_bound = [5000, 5000, 200, 200];
% Objective function for optimization (minimize vertical acceleration)
objective = @(x) objectiveFunction(x, k1, k2, c1, c2);
% Initial guess for optimization variables [k1, k2, c1, c2]
x0 = [3000, 2500, 150, 100];
% Optimization options
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'interior-point');
% Perform optimization
[x_opt, fval] = fmincon(objective, x0, [], [], [], [], lower_bound, upper_bound, [], options);
% Display optimized results
disp('Optimized Spring Constants and Damping Coefficients:');
disp(['k1: ', num2str(x_opt(1)), ' N/m']);
disp(['k2: ', num2str(x_opt(2)), ' N/m']);
disp(['c1: ', num2str(x_opt(3)), ' Ns/m']);
disp(['c2: ', num2str(x_opt(4)), ' Ns/m']);
disp(['Optimized Objective Value: ', num2str(fval)]);
% Simulate the system with the optimized parameters and obtain acceleration before optimization
[x_sim_before, v_sim_before, a_sim_before] = simulateQuarterCarModel(m1, m2, k1, k2, c1, c2);
% Simulate the system with the optimized parameters and obtain acceleration after optimization
[x_sim_after, v_sim_after, a_sim_after] = simulateQuarterCarModel(m1, m2, x_opt(1), x_opt(2), x_opt(3), x_opt(4));
% Plot results
figure;
% Plot acceleration before optimization
subplot(3, 1, 1);
plot(t, a_sim_before, 'b', 'LineWidth', 1.5);
title('Acceleration of Sprung Mass (Before Optimization)');
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
grid on;
% Plot acceleration after optimization
subplot(3, 1, 2);
plot(t, a_sim_after, 'r', 'LineWidth', 1.5);
title('Acceleration of Sprung Mass (After Optimization)');
xlabel('Time (s)');
ylabel('Acceleration (m/s^2)');
grid on;
% Plot road profile
subplot(3, 1, 3);
plot(t, road_profile(t), 'g', 'LineWidth', 1.5);
title('Road Profile');
xlabel('Time (s)');
ylabel('Road Profile');
grid on;
function J = objectiveFunction(x, m1, m2, ~, ~)
% Simulate the system and compute the objective function
[~, ~, a_sim] = simulateQuarterCarModel(m1, m2, x(1), x(2), x(3), x(4));
% Objective: Minimize the vertical acceleration of the sprung mass
J = max(abs(a_sim));
end
function [x_sim, v_sim, a_sim] = simulateQuarterCarModel(m1, ~, k1, k2, c1, c2)
% Simulation parameters
dt = 0.01; % time step (s)
t_end = 5; % simulation time (s)
t = 0:dt:t_end;
% Preallocate arrays
x_sim = zeros(size(t));
v_sim = zeros(size(t));
a_sim = zeros(size(t));
% Initial conditions
x_sim(1) = 0.1; % initial displacement of the sprung mass (m)
v_sim(1) = 10; % initial velocity of the sprung mass (m/s)
% Simulation loop
for i = 1:length(t)-1
% State-space equations
A = [0, 1; -((k1 + k2)/m1), -(c1 + c2)/m1];
B = [0; k2/m1];
u = road_profile(t(i)); % road profile as an input
% Update state using Euler integration
x_dot = A * [x_sim(i); v_sim(i)] + B * u;
x_sim(i+1) = x_sim(i) + v_sim(i) * dt;
v_sim(i+1) = v_sim(i) + x_dot(2) * dt;
a_sim(i+1) = x_dot(2);
end
end
function y = road_profile(t)
% Define a simple road profile function (you can replace this with your own)
y = 0.1 * sin(2 * pi * 0.5 * t);
end
I write this and end up to the following:
Error in newopt>@(x)objectiveFunction(x,k1,k2,c1,c2) (line 8)
objective = @(x) objectiveFunction(x, k1, k2, c1, c2);
Error in fmincon (line 573)
initVals.f = feval(funfcn{3},X,varargin{:});
Error in newopt (line 17)
[x_opt, fval] = fmincon(objective, x0, [], [], [], [], lower_bound, upper_bound, [], options);
Caused by:
Failure in initial objective function evaluation. FMINCON cannot continue.
2 个评论
Dyuman Joshi
2023-12-5
编辑:Dyuman Joshi
2023-12-5
The error is clear, k1 is undefined. So are k2, c1, and c2.
lower_bound = [1000, 1000, 50, 50];
upper_bound = [5000, 5000, 200, 200];
% Objective function for optimization (minimize vertical acceleration)
% vv vv vv vv
objective = @(x) objectiveFunction(x, k1, k2, c1, c2);
It's not clear to me what is being optimized.
采纳的回答
Sam Chak
2023-12-6
Hi @SOUVIK
The input argument to call fmincon() is incomplete. I prefer the ode45 solver over the Euler integration method. However, you can still use your own user-defined models to simulate before and after optimization.
%% Optimization of Spring Constants and Damping Coefficients for Minimum Acceleration
%% Settings
objfun = @costfun; % tag the objective function
x0 = [3000, 2500, 150, 100]; % Initial guess for [k1, k2, c1, c2]
nvars = 4; % number of optimization variables
A = - eye(nvars);
b = zeros(nvars, 1); % A*x <= b
Aeq = []; % nil
beq = []; % nil
lb = [1000, 1000, 50, 50]; % lower bound
ub = [5000, 5000, 200, 200]; % upper bound
nonlcon = []; % nil
options = optimoptions('fmincon', 'Display', 'iter', 'Algorithm', 'interior-point');
%% Call fmincon to solve the optimization problem
[x_opt, fval] = fmincon(objfun, x0, A, b, Aeq, beq, lb, ub, nonlcon, options);
%% Display optimized results
disp('Optimized Spring Constants and Damping Coefficients:');
disp(['k1: ', num2str(x_opt(1)), ' N/m']);
disp(['k2: ', num2str(x_opt(2)), ' N/m']);
disp(['c1: ', num2str(x_opt(3)), ' Ns/m']);
disp(['c2: ', num2str(x_opt(4)), ' Ns/m']);
disp(['Optimized Objective Value: ', num2str(fval)]);
%% Objective Function
function J = costfun(param) % cost function must contain the parameters to be optimized
% first things first is to define the parameters
k1 = param(1); % spring constant of shock absorber 1
k2 = param(2); % spring constant of shock absorber 2
c1 = param(3); % damping coeff of shock absorber 1
c2 = param(4); % damping coeff of shock absorber 2
tspan = [0 10];
x0 = [0.1; 10];
[t, x] = ode45(@(t, x) odefcn(t, x, k1, k2, c1, c2), tspan, x0);
for j = 1:length(t)
dxdt(j,:) = odefcn(t(j), x(j,:).', k1, k2, c1, c2);
end
% double check if this maximum absolute acceleration is to minimize it
J = max(abs(dxdt(:,2)));
end
%% Quarter Car Model
function dxdt = odefcn(t, x, k1, k2, c1, c2) % mass m2 is missing!!
m1 = 1500; % [kg] average car weight
A = [ 0, 1;
-(k1 + k2)/m1, -(c1 + c2)/m1];
B = [ 0;
k2/m1];
u = 0.1*sin(2*pi*0.5*t); % road profile as an input
dxdt = A*x + B*u;
end
3 个评论
Sam Chak
2023-12-6
Hi @SOUVIK
That value comes from the Objective Function:
J = max(abs(dxdt(:,2)));
which should be equivalent to yours:
J = max(abs(a_sim));
Basically, fmincon found that , , , return the minimum maximal value of the absolute acceleration, .
However, since the optimal values of the parameters lie on the boundaries, you may need to either shift the lower and upper bounds, revisit/redesign the objective function, or do both.
更多回答(1 个)
Torsten
2023-12-5
编辑:Torsten
2023-12-5
You don't supply k1, k2, c1 and c2 to the objective function.
If these are the parameters to be optimized, you don't need to pass them since they are stored in the vector x.
3 个评论
Steven Lord
2023-12-6
You can't simply leave out inputs you don't want to specify or pass the inputs in an arbitrary order when you call fmincon. The way you're calling it:
[x_opt, fval] = fmincon(objective, x0, lower_bound, upper_bound, [], options);
attempts to pass the variable lower_bound in the place fmincon expects the left-hand side of the linear inequality constraints, upper_bound in the place fmincon expects the right-hand side of the linear inequality constraints, and options in the place where fmincon expects the right-hand side of the linear equality constraints. fmincon doesn't "look at the names" or anything like that and try to guess what you meant, it does what you told it to do.
If you don't want to specify one or more of those inputs, you must include a [] in place of the input you don't want to specify.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Problem-Based Optimization and Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!