Seeking help with gradient descent optimization for Nonlinear Transmission Line

22 次查看(过去 30 天)
I am trying to optimize the parameters L and Cjo of this NLTL circuit using Gradient Descent Optimization. I am trying to get a peak voltage of 20V. But I am getting unrealistic values and iteration is not running for more than once. For Example, this is an output I am getting:
Iteration 1: Error = 124.4584
Optimized Parameters:
1.0e-05 *
0.2700 0.0000
I am not sure if I am defining the desired result or the compute gradient correctly. Could someone help me fix the issues? I am providing the circuit and the type of waveform I want to generate with peak of 20V. I am providing the code that I have:
% Define initial parameters
params = [2.7e-6, 61.3e-12]; % [L_value, Cjo_value]
learning_rate = [1e-3, 1e-6]; % Separate learning rates for L and Cjo
tolerance = 1e-9;
max_iterations = 10;
desired_error_limit = 1e-5;
% Objective function to minimize
objective_function = @(params) run_simulation_and_get_error(params);
% Gradient descent loop
for iter = 1:max_iterations
% Calculate the current error
current_error = objective_function(params);
% Compute the gradient for each parameter
gradient = compute_gradient(params, objective_function);
% Update parameters using gradient descent with different learning rates
params(1) = params(1) - learning_rate(1) * gradient(1); % L_value update
params(2) = params(2) - learning_rate(2) * gradient(2); % Cjo_value update
% Check for convergence
if norm(gradient) < tolerance || current_error <= desired_error_limit
if current_error <= desired_error_limit
disp('Converged based on desired error limit');
else
disp('Converged based on gradient norm');
end
break;
end
fprintf('Iteration %d: Error = %.4f\n', iter, current_error);
end
Error using fread
Invalid file identifier. Use fopen to generate a valid file identifier.

Error in solution>update_netlist_file (line 63)
fileContent = fread(fid, '*char')';

Error in solution>run_simulation_and_get_error (line 49)
update_netlist_file(netlistFile, L_value, Cjo_value);

Error in solution>@(params)run_simulation_and_get_error(params) (line 8)
objective_function = @(params) run_simulation_and_get_error(params);
% Final optimized parameters
fprintf('Iteration %d: Error = %.4f\n', iter, current_error);
disp('Optimized Parameters:');
disp(params);
% Function to run LTspice simulation and get error
function error = run_simulation_and_get_error(params)
% Parameters
L_value = params(1); % Extract L from params
Cjo_value = params(2); % Extract Cjo from params
% File paths
netlistFile = ['C:\Users\s007s364\Downloads\'...
'LTspice_2_Matlab\oscillatory.net'];
rawFile = ['C:\Users\s007s364\Downloads\'...
'LTspice_2_Matlab\oscillatory.raw'];
ltspiceExe = 'C:\Program Files\ADI\LTspice\LTspice.exe';
% Update the netlist file with the new L and Cjo values
update_netlist_file(netlistFile, L_value, Cjo_value);
% Run LTspice simulation
run_ltspice_simulation(ltspiceExe, netlistFile);
% Extract results from LTspice output
simulationResults = extract_simulation_results(rawFile);
% Get the desired target waveform
targetWaveform = get_desired_results(simulationResults);
% Compute the error between simulation results and the target waveform
error = compute_error(simulationResults, targetWaveform);
end
% Function to update the netlist with new L and Cjo values
function update_netlist_file(netlistFile, L_value, Cjo_value)
% Read the netlist file
fid = fopen(netlistFile, 'r');
fileContent = fread(fid, '*char')';
fclose(fid);
% Update the L parameter in the netlist
newContent = regexprep(fileContent, 'L\s*=\s*\d+\.?\d*e?-?\d*', sprintf('L=%.6f', L_value));
% Update the Cjo parameter in the .model statement
newContent = regexprep(newContent, 'Cjo\s*=\s*\d+\.?\d*e?-?\d*', sprintf('Cjo=%.6fe-12', Cjo_value)); % Convert Cjo to picofarads
% Write the updated content back to the netlist file
fid = fopen(netlistFile, 'w');
fwrite(fid, newContent);
fclose(fid);
end
% Function to run LTspice simulation
function run_ltspice_simulation(ltspiceExe, netlistFile)
% Execute LTspice simulation
command = sprintf('"%s" -Run -b "%s"', ltspiceExe, netlistFile);
disp(['Command: ', command]);
[status, cmdout] = system(command);
if status ~= 0
error('LTspice simulation failed: %s', cmdout);
else
disp('LTspice simulation completed successfully.');
end
% Pause to ensure the simulation has time to complete
pause(5);
end
% Function to extract simulation results
function results = extract_simulation_results(rawFile)
% Use LTspice2Matlab or another method to read the .raw file
raw_data = LTspice2Matlab(rawFile);
variable_to_plot = 22; % Adjust based on your netlist output variable
voltage = raw_data.variable_mat(variable_to_plot, :); % Extract voltage data
results = voltage; % Return the full waveform (not just max voltage)
end
% Function to compute the error between simulation and desired results
function error = compute_error(simulationResults, targetWaveform)
% Compute Mean Squared Error (MSE) between simulated and target waveform
error = mean((simulationResults - targetWaveform).^2);
end
% Function to create a target waveform that peaks at 20V
function targetWaveform = get_desired_results(simulationResults)
% Scale the waveform to peak at 20V based on the structure of simulation output
max_simulation_voltage = max(simulationResults);
scaling_factor = 20 / max_simulation_voltage; % Scale to 20V peak
targetWaveform = simulationResults * scaling_factor;
end
% Function to compute gradient (finite differences)
function grad = compute_gradient(params, objective_function)
delta = [1e-6, 1e-9]; % Set appropriate delta values for L and Cjo
grad = zeros(size(params));
for i = 1:length(params)
params_plus = params;
params_plus(i) = params_plus(i) + delta(i); % Use appropriate delta for each parameter
grad(i) = (objective_function(params_plus) - objective_function(params)) / delta(i);
end
end

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Generate MATLAB Code in Diagnostic Feature Designer 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by