Please help for objectiveFunction command.
显示 更早的评论
% 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.
采纳的回答
更多回答(1 个)
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.
类别
在 帮助中心 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!