Storing parameters from a for loop in arrays

1 vue (au cours des 30 derniers jours)
Matt Boyles
Matt Boyles le 31 Mai 2023
Modifié(e) : Torsten le 1 Juin 2023
Hi,
I am trying to apply the Gauss-Newton method with a set of data and then use the theta parameters found and plot these over my number of iterations. The values should converge but at the moment my data and plot are not working with my code and I think it has to do with my set up of the array storage. Could someone please help debug my code.
Thank you!!
% Gauss Newton 1
clear
clc
close all
% --- Task 3 ---%
% h = importdata('LLS_Data1.mat');
% t = h(:, 1);
% y = h(:, 2);
t = [0:0.2:3].';
y = [3.43101641262231;-0.527550212558249;3.43697613505530;0.186721377613475;5.11301585254667;
6.78626419728379;9.69387139109200;7.94303873884149;14.8277080957188;19.2249360936885;20.4921937831721;
14.2323004458775;21.8155194641984;21.1095884066726;37.9339164089595;41.1133734753079];
% Set iterations and initial guess
n = length(t);
iter = 10;
initialguess = [0; 0];
theta = initialguess;
% Initialise Arrays to Store Parameter Values and Iteration Number
theta1 = zeros(iter,1);
theta2 = zeros(iter,1);
theta1(:,1) = initialguess(1);
theta2(:,2) = initialguess(2);
for i = 1: iter
% Model Equation
yM = theta(1)*exp(t) - theta(2);
%Compute Error (data - model equation)
E = y - yM;
% Compute Jacobian (grad E)
J = [-exp(t), -ones(n,1)];
delta_theta = theta - inv((J')*(J)) * (J')*(E);
% Update Theta Value
theta = delta_theta + theta;
theta1(i, :) = theta(1);
theta2(i, :) = theta(1);
end
theta = [theta1(1);theta2(1)];
%--- Task 4 ---%
% Plot Identified Parameters
figure(1);
plot(1:iter, theta1(1))
hold on
plot(1:iter, theta2(2))
xlabel('Iteration');
ylabel('Parameter Value');
legend('Theta 1', 'Theta 2');
title("Identified Parameters vs Iterations");
grid on;

Réponses (2)

Walter Roberson
Walter Roberson le 31 Mai 2023
You have
theta1(i, :) = theta(1);
theta2(i, :) = theta(1);
Notice you are storing theta(1) in both cases. You should be storing theta(2) for one of the two.
  2 commentaires
Matt Boyles
Matt Boyles le 31 Mai 2023
Correct thanks for spotting that!
The code however is still not iterating properly and getting the convergence I am after...
Walter Roberson
Walter Roberson le 1 Juin 2023
Modifié(e) : Walter Roberson le 1 Juin 2023
% --- Task 3 ---%
% h = importdata('LLS_Data1.mat');
% t = h(:, 1);
% y = h(:, 2);
t = [0:0.2:3].';
y = [3.43101641262231;-0.527550212558249;3.43697613505530;0.186721377613475;5.11301585254667;
6.78626419728379;9.69387139109200;7.94303873884149;14.8277080957188;19.2249360936885;20.4921937831721;
14.2323004458775;21.8155194641984;21.1095884066726;37.9339164089595;41.1133734753079];
% Set iterations and initial guess
n = length(t);
iter = 10;
initialguess = [0; 0];
theta = initialguess;
% Initialise Arrays to Store Parameter Values and Iteration Number
theta1 = zeros(iter,1);
theta2 = zeros(iter,1);
theta1(:,1) = initialguess(1);
theta2(:,2) = initialguess(2);
for i = 1: iter
% Model Equation
yM = theta(1)*exp(t) - theta(2);
%Compute Error (data - model equation)
E = y - yM;
% Compute Jacobian (grad E)
J = [-exp(t), -ones(n,1)];
delta_theta = theta - inv((J')*(J)) * (J')*(E);
% Update Theta Value
theta = delta_theta + theta;
theta1(i, :) = theta(1);
theta2(i, :) = theta(2);
end
theta = [theta1(i);theta2(i)];
%--- Task 4 ---%
% Plot Identified Parameters
figure(1);
subplot(2,1,1);
plot(1:iter, theta1, 'k')
xlabel('Iteration');
ylabel('Parameter Value');
title('Theta 1')
grid on
subplot(2,1,2)
plot(1:iter, theta2, 'b')
xlabel('Iteration');
ylabel('Parameter Value');
title("Theta 2");
grid on;

Connectez-vous pour commenter.


Torsten
Torsten le 1 Juin 2023
Modifié(e) : Torsten le 1 Juin 2023
t = [0:0.2:3].';
y = [3.43101641262231;-0.527550212558249;3.43697613505530;0.186721377613475;5.11301585254667;
6.78626419728379;9.69387139109200;7.94303873884149;14.8277080957188;19.2249360936885;20.4921937831721;
14.2323004458775;21.8155194641984;21.1095884066726;37.9339164089595;41.1133734753079];
% Set iterations and initial guess
n = length(t);
iter = 10;
initialguess = [0; 0];
% Initialise Arrays to Store Parameter Values and Iteration Number
theta = zeros(2,iter+1);
theta(:,1) = initialguess;
for i = 1:iter
% Model Equation
yM = theta(1,i)*exp(t) - theta(2,i);
%Compute Error (data - model equation)
E = y - yM;
% Compute Jacobian (grad E)
J = [-exp(t), ones(n,1)];
delta_theta = - inv((J')*(J)) * (J')*(E);
% Update Theta Value
theta(:,i+1) = theta(:,i) + delta_theta;
end
%--- Task 4 ---%
% Plot Identified Parameters
figure(1);
plot(1:iter+1, [theta(1,:);theta(2,:)])
xlabel('Iteration');
ylabel('Parameter Value');
legend('Theta 1', 'Theta 2');
title("Identified Parameters vs Iterations");
grid on;
figure(2)
hold on
plot(t,y,'o')
plot(t,theta(1,end)*exp(t) - theta(2,end))
hold off
grid on

Catégories

En savoir plus sur Sparse Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by