How can I fix the error in my MATLAB program's objective function, which aims to optimize Simulink model parameters to match simulated data with experimental data (y_exp)?
2 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Max
am 16 Nov. 2023
Kommentiert: Torsten
am 29 Nov. 2023
I am trying to code a Matlab program that optimizes the parameters of a Simulink model called "R1Model" and simultaneously compares the simulation data (simulated_data) generated by the model with the experimental data (y_exp). Once the optimization is complete, the code should display the values of the optimized parameters such that the simulated data matches the real data. I think the error is in the objective function. This Error message always occurs:
Error using fmincon (line 321)
Row dimension of A is inconsistent with length of b.
Error in Try1 (line 27)
[optimal_param, optimal_value] = fmincon(objective_function, initialRCParams, lb, ub);
Help is highly appreciated:)
Thanks!
The Code:
%load('Res_1st_run.mat');
ExpData = importdata("Res_1st_run.mat");
t_exp = ExpData.results.I_cmd_A.Time.';
%y_exp = ExpData.results.U_measured_V.Data.';
y_exp = ExpData.results.SOC_BAT.Data.'./100;
% the Simulink model function to be optimized
simulink_model = @(param) simulate_model(param, t_exp);
% the objective function to minimize (sum of squared differences)
objective_function = @(param) sum((y_exp - simulink_model(param)).^2);
% Initial parameter guess
initialRCParams = [RCParameters.R_i, RCParameters.R_1, RCParameters.R_2, RCParameters.C_1, RCParameters.C_2];
% Define parameter bounds
lb = double([0, 0, 0, 0, 0]);
ub = double([10, 10, 10, 10, 10]);
% Optimization using fmincon
% options = optimset('Display', 'iter', 'MaxIter', 100); % Modify options as needed
[optimal_param, optimal_value] = fmincon(objective_function, initialRCParams, lb, ub);
% Display optimized parameter and objective value
fprintf('Optimized Parameter: %.4f\n', optimal_param);
fprintf('Optimized Objective Value: %.4f\n', optimal_value);
% Simulate the model with the optimized parameter
simulated_data = simulate_model(optimal_param, t_exp);
% Plot the experimental and simulated data for comparison
plot(t_exp, y_exp, 'b', t_exp, simulated_data, 'r');
xlabel('Time');
ylabel('Data');
legend('Experimental Data', 'Simulated Data');
function simulated_data = simulate_model(param, t_exp)
open_system('R1Model');
%param = initialRCParams;
RCParameters = param;
% Simulate the model and return the simulated data
simulated_data = sim('R1Model', 'StartTime', num2str(t_exp(1)), 'StopTime', num2str(t_exp(end)));
simulated_data = simulated_data.yout{1}.Values.Data;
end
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 16 Nov. 2023
[optimal_param, optimal_value] = fmincon(objective_function, initialRCParams, lb, ub);
Mathworks-provided functions never look at the names of variables that you are passing in as parameters in order to guess what functional role the contents of those parameters have.
Mathworks-provided functions work with a mix of positional parameters and name-value pairs (all name-value pairs must go after positional parameters.)
fmincon() does not accept any name-value pairs, only positional parameters. If you do not wish to actively use a particular positional parameter, pass in [ ] in that location.
[optimal_param, optimal_value] = fmincon(objective_function, initialRCParams, [], [], [], [], lb, ub);
4 Kommentare
Torsten
am 29 Nov. 2023
Did you try a different initial point ?
Did you write the simulated data to file during execution and compare them with those you obtain with the initial parameters ?
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Estimate Parameters and States finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!