Clear Filters
Clear Filters

Optimization toolbox for multiparameter optimization of a custom equation

4 views (last 30 days)
Hi, I developed (with the immense help of mathworks community, hats off!) the below for inverse parameter optimization as a part of my doctoral studies. I have five parameters to optimize. And Here I have provided the function as well. I want to know whether this can be done in optimization toolbox live script solver based script. I am new to that and few attempts made were unsuccesful. Because, I think that is more advanced and could try different optimization algorithms from that. Pls help me!
MAIN FILE
rng default % For reproducibility
% Constants
AD = 1.2; % Air density
DV = 1.84e-5; % Dynamic viscosity
p0 = 101320; % Air pressure
SH = 1.4; % Specific heat of air
PN = 0.7; % Prandtl constant
c0 = 343; % Air velocity
d = 0.05; % Assuming a constant value for d
% Define the model equation
SAC_fun = @(x, f) your_SAC_function(x, f, AD, DV, p0, SH, PN, c0, d);
% Load experimental data from Excel file
exp_data = readmatrix('/Users/pramodya/Desktop/PhD/Modelling/MATLAB Final codes/My research/Exp.results/Done/Control.xlsx'); % Assuming first column contains frequencies and second column contains corresponding SAC values
f_values = exp_data(:, 1); % Frequency data
ydata = exp_data(:, 2); % SAC experimental data
% Plot the experimental data
plot(f_values, ydata, 'r*')
xlabel 'Frequency'
ylabel 'SAC'
title('Experimental Data')
% Define lsqcurvefit problem with adjusted initial guesses and bounds
x0 = [0.003, 0.0005, 1, 5000, 0.1]; % Initial guess for parameters
lb = [0,0,0,0,0]; % Lower bounds for parameters
ub = [inf, inf, inf, inf, 1]; % Upper bounds for parameters
options = optimoptions('lsqcurvefit', 'Display', 'iter');
% Fit the model to the data
[sol, resnorm] = lsqcurvefit(SAC_fun, x0, f_values, ydata, lb, ub, options);
% Plot the fitted curve
figure
responsedata = SAC_fun(sol, f_values);
plot(f_values, ydata, 'y*', f_values, responsedata, 'g-')
legend('Experimental Data', 'Fitted Curve')
xlabel 'Frequency'
ylabel 'SAC'
title('Fitted Response')
% Display optimized parameter values
disp('Optimized Parameter Values:');
disp(['VL: ', num2str(sol(1))]);
disp(['TL: ', num2str(sol(2))]);
disp(['T: ', num2str(sol(3))]);
disp(['FR: ', num2str(sol(4))]);
disp(['P: ', num2str(sol(5))]);
% Calculate R-squared value
SS_total = sum((ydata - mean(ydata)).^2);
SS_residual = sum(resnorm.^2);
R_squared = 1 - (SS_residual / SS_total);
% Display R-squared value
disp(['R-squared Value: ', num2str(R_squared)]);
FUNCTION FILE
function SAC = your_SAC_function(x, f_values, AD, DV, p0, SH, PN, c0, d)
% Parameters
VL = x(1);
TL = x(2);
T = x(3);
FR = x(4);
P = x(5);
% SAC calculation based on provided equations
E3 = sqrt(1 + (4 * 1i * T * T * DV * 2 * pi * f_values * AD) / (FR * FR * VL * VL * P * P));
E4 = FR * P ./ (1i * 2 * pi * f_values * AD * T);
E1 = T * AD .* (1 + E4 .* E3);
E6 = 1i * 8 * 2 * pi * f_values ./ (PN * AD * 2 * pi * f_values * TL * TL);
E7 = sqrt(1 + (1i * PN * AD * 2 * pi * f_values * TL * TL) / (16 * DV));
E8 = 1./(1 - (E6 .* E7));
E2 = (SH * p0) ./ (SH - ((SH - 1).* E8));
KC = 2 * pi * f_values .* sqrt(E1 ./ E2);
ZC = sqrt(E1 .* E2);
ZS = -1i * ZC .* cot(KC .* d) / P;
R = (ZS - AD * c0) ./ (ZS + AD * c0);
SAC = 1 - abs(R).^2;
end
  2 Comments
Torsten
Torsten on 2 May 2024
Edited: Torsten on 2 May 2024
You didn't explain what the problem with the above code is. Aren't you able to execute it ? Do you get an error message ?
Pramodya
Pramodya on 2 May 2024
Edited: Pramodya on 2 May 2024
Hi Torsten,
Actually the given code perfectly works. I just want to know how to do the same in optimization live script using solver-based section?

Sign in to comment.

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!