What is this error and how do I fix it?
2 views (last 30 days)
Show older comments
% 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);
% Display optimized values
disp('Optimized Values:');
disp([' VL: ', num2str(optimized_params(1))]);
disp([' TL: ', num2str(optimized_params(2))]);
disp([' T: ', num2str(optimized_params(3))]);
disp([' FR: ', num2str(optimized_params(4))]);
% 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:');
disp([' 125 Hz: ', num2str(optimized_SAC(1))]);
disp([' 250 Hz: ', num2str(optimized_SAC(2))]);
disp([' 500 Hz: ', num2str(optimized_SAC(3))]);
disp([' 1000 Hz: ', num2str(optimized_SAC(4))]);
disp([' 2000 Hz: ', num2str(optimized_SAC(5))]);
disp([' 4000 Hz: ', num2str(optimized_SAC(6))]);
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 Comments
Dyuman Joshi
on 29 Jan 2024
The code seems to be working here without any errors, see the edit above.
Accepted Answer
Dyuman Joshi
on 29 Jan 2024
Moved: Dyuman Joshi
on 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 Comments
Dyuman Joshi
on 30 Jan 2024
@Pramodya - You're welcome!
If this answer solved your problem, please consider accepting the answer.
More Answers (0)
See Also
Categories
Find more on Direct Search in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!