What is this error and how do I fix it?

2 vues (au cours des 30 derniers jours)
Pramodya
Pramodya le 29 Jan 2024
Commenté : Dyuman Joshi le 30 Jan 2024
% Set actual constant parameters
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
% Create a vector of frequencies from 0 to 7000 Hz with a step of 1 Hz
frequencies = [125, 250, 500, 1000, 2000, 4000];
% Target SAC values for each frequency
target_SAC = [0.117, 0.164, 0.570, 0.948, 0.964, 0.999];
% Define the function to minimize (residuals)
objective_function = @(params) calculate_residuals(params, frequencies, target_SAC, AD, DV, p0, SH, PN, c0);
% Initial guess for parameters
initial_guess = [0.003, 0.003, 10, 1e5]; % Initial values for VL, TL, T, FR
% Set lower and upper bounds for parameters
lb = [0.001, 0.001, 1, 1e-3];
ub = [0.005, 0.005, 20, 1e8];
% Optimize using lsqnonlin
optimized_params = lsqnonlin(objective_function, initial_guess, lb, ub);
Local minimum possible. lsqnonlin stopped because the size of the current step is less than the value of the step size tolerance.
% Display optimized values
disp('Optimized Values:');
Optimized Values:
disp([' VL: ', num2str(optimized_params(1))]);
VL: 0.0049655
disp([' TL: ', num2str(optimized_params(2))]);
TL: 0.0010096
disp([' T: ', num2str(optimized_params(3))]);
T: 1.2337
disp([' FR: ', num2str(optimized_params(4))]);
FR: 0.095723
% Calculate SAC with the optimized parameters
optimized_SAC = calculate_SAC(optimized_params, frequencies, AD, DV, p0, SH, PN, c0);
% Display SAC values with the optimized parameters
disp('Optimized SAC Values:');
Optimized SAC Values:
disp([' 125 Hz: ', num2str(optimized_SAC(1))]);
125 Hz: 0.063996
disp([' 250 Hz: ', num2str(optimized_SAC(2))]);
250 Hz: 0.22146
disp([' 500 Hz: ', num2str(optimized_SAC(3))]);
500 Hz: 0.57354
disp([' 1000 Hz: ', num2str(optimized_SAC(4))]);
1000 Hz: 0.92844
disp([' 2000 Hz: ', num2str(optimized_SAC(5))]);
2000 Hz: 0.94591
disp([' 4000 Hz: ', num2str(optimized_SAC(6))]);
4000 Hz: 0.97167
function residuals = calculate_residuals(params, frequencies, target_SAC, AD, DV, p0, SH, PN, c0)
% Calculate SAC with given parameters
calculated_SAC = calculate_SAC(params, frequencies, AD, DV, p0, SH, PN, c0);
% Calculate residuals (difference between calculated and target SAC)
residuals = calculated_SAC - target_SAC;
end
function SAC = calculate_SAC(params, frequencies, AD, DV, p0, SH, PN, c0)
% Extract parameters
VL = params(1);
TL = params(2);
T = params(3);
FR = params(4);
% Additional constants
d = 0.060; % Assuming a constant value for d
P = 0.977; % Assuming a constant value for P
% Initialize SAC array
SAC = zeros(size(frequencies));
% Loop through different frequencies
for i = 1:numel(frequencies)
f = frequencies(i);
% Evaluate equations for the current set of parameters
E3 = sqrt(1 + (4 * 1i * T * T * DV * 2 * pi * f * AD) / (FR * FR * VL * VL * p0 * p0));
E4 = FR * p0 ./ (1i * 2 * pi * f * AD * T);
E1 = T * AD .* (1 + E4 .* E3);
E6 = 1i * 8 * 2 * pi * f ./ (PN * AD * 2 * pi * f * TL * TL);
E7 = sqrt(1 + (1i * PN * AD * 2 * pi * f * TL * TL / 16 * DV));
E8 = 1./(1 - (E6 .* E7));
E2 = (SH * p0) ./ (SH - ((SH - 1).* E8));
KC = 2 * pi * f .* sqrt(E1 ./ E2);
ZC = sqrt(E1 .* E2);
ZS = -1i * ZC .* cot(KC .* d) / P;
R = (ZS - AD * c0) ./ (ZS + AD * c0);
SAC(i) = 1 - abs(R).^2;
end
end
Unrecognized function or variable 'calculate_residuals'.
Error in @(params)calculate_residuals(params,frequencies,target_SAC,AD,DV,p0,SH,PN,c0)
Error in lsqnonlin (line 242)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});
  2 commentaires
Dyuman Joshi
Dyuman Joshi le 29 Jan 2024
The code seems to be working here without any errors, see the edit above.
Pramodya
Pramodya le 29 Jan 2024
Thank you very much for providing a solution. However, it gives the below error. Do I need to download additional plugins as this errors is appearing.
Unrecognized function or variable 'calculate_residuals'.
Error in @(params)calculate_residuals(params,frequencies,target_SAC,AD,DV,p0,SH,PN,c0)
Error in lsqnonlin (line 242)
initVals.F = feval(funfcn{3},xCurrent,varargin{:});

Connectez-vous pour commenter.

Réponse acceptée

Dyuman Joshi
Dyuman Joshi le 29 Jan 2024
Déplacé(e) : Dyuman Joshi le 29 Jan 2024
Most likely the function "calculate_residuals" is not in the current directory.
Type "cd" in the command window to get the current directory, check whether the function is present there or not. Check for "calculate_SAC" as well.
If they are not present there, you should move them to the current directory or add them to the current path.
  2 commentaires
Pramodya
Pramodya le 29 Jan 2024
Déplacé(e) : Dyuman Joshi le 29 Jan 2024
Thank you very much. It worked!! Followed the instructions and saved all files in the current directory. Thank you again!A great support!
Dyuman Joshi
Dyuman Joshi le 30 Jan 2024
@Pramodya - You're welcome!
If this answer solved your problem, please consider accepting the answer.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by