Why is this newton raphson outputting the same number? How can I change this?

1 view (last 30 days)
Im trying to use the data from each variable calculated to produce a plot, but I keep getting the same number output over the 360 iterations. How can I change it to make the output numbers change over each iteration? Does anyone know why the for loop is not working??
% Clearing all values to ensure code runs properly
clc; clear;
% Initiating timer
tic
% Setting number of data points 'n' to 100 as to analyze 100 incrimiates of
% θ_2
% between 0 and 2PI
n = 360;
% Defining known variables R1, R2, R3, R4, Rbc and Rbe in meters
r1=0;
r2 = 1.402;
r3 = 0.701;
r4 = 0.702;
rbc = 0.5;
rbe = 0.5;
% Defining known and angle theta1 and setting input
% angle of θ_2 to have 'n' number of incriminates from 0 to 2PI
th1 = 0;
th2 = linspace(0,2*pi,n);
% Defining inital guesses for each unknown value theta3,theta4,theta5,
% and R5 in radians/meters
th3guess = -0.87;
th4guess = 0.87;
th5guess = 0.9;
r5guess = 1.2;
% Pre allocated a nx4 matrix of zeros to improve speed. This is done because
% as each position of θ_2 is solved, 4 variables (th3,th4,th5,R5)
% will be solved.
m = zeros(n,4);
% Beginning 'for' loop to iterativly solve for each unknown variable,
% evaluating each postion of theta 2 from postion in column 1 to column 'n'
% This iterative process is done using the NR function which is a Newton
% Raphson function definied later in the function section.
for i = 1:n
% In this function th3, th4, th5, and R5 are outputs to the function and
% inital guesses,known values, and 'n' positions of θ_2 are inputs
[th3,th4,th5,r5] = NR(th1,th2(i),th3guess,th4guess,th5guess,r1,r2,r3,r4,r5guess,rbc,rbe);
% Each loop produces th3, R3, R4, and R5 for the correspoinding
% position of th2
th3guess = th3;
th4guess = th4;
th5guess = th5;
r5guess = r5;
% 'm(i,:)' saves output data from each loop into a matrix for ease of
% analysis
m(i,:) = [th3,th4,th5,r5];
end
th2plot = th2';
th3plot = (m(:,1));
th4plot = (m(:,2));
th5plot = (m(:,3));
r5plot = (m(:,4));
function [th3,th4,th5,r5] = NR(~,~,th3guess,th4guess,th5guess,~,r2,r3,r4,r5guess,rbc,rbe)
% Function 'NR' is a Newton Raphson function that inputs known/defined
% variables, an input variable 'th2', as well as inital guesses for unknown
% function uses Ax = b --> x = A\b (backslash method) to iteratively produce
% and update guesses until the output meets the defined convergence criterion
% for convergence is met, the solution of unknown 'x' array of variables is
% Setting criterion for convergence as 1e-6
epsilon = 1e-6;
% Initial residual matrix formulation
f = [-(0 + r3.*cos(th3guess) - r4.*cos(th4guess));...
-(r2 + r3.*sin(th3guess) - r4.*sin(th4guess));...
-(rbc.*cos(th3guess) + r5guess.*cos(th5guess) - rbe.*cos(th4guess));...
-(rbc.*sin(th3guess) + r5guess.*sin(th5guess) - rbe.*sin(th4guess))];
% iteration counter begins at 0
iter = 0;
% while statement confirming convergence criterion using 'norm' function
% for residual
while norm(f)>epsilon
% for each 'while' loop iteration 1 is added to iteration counter
iter = iter + 1;
% Jacobian for data is set to be evaluated
J = [-r3.*sin(th3guess),r4.*sin(th4guess),0,0;...
r3.*cos(th3guess),-r4.*cos(th4guess),0,0;...
-rbc.*sin(th3guess),rbe.*sin(th4guess),-r5guess.*sin(th5guess),-cos(th5guess);...
rbc.*cos(th3guess),-rbe.*cos(th4guess),r5guess.*cos(th5guess),sin(th5guess)];
% change in all variabels in displayed by 'dth' which builds a matrix
% of values to be added to inital guesses.
dth = J\f;
th3guess = th3guess+dth(1);
th4guess = th4guess+dth(2);
th5guess = th5guess+dth(3);
r5guess = r5guess+dth(4);
% residual is now redifined with updated guesses and loop continues
% until convergence is reached and unknown 'x' values are produced.
f = [-(0 + r3.*cos(th3guess) - r4.*cos(th4guess));...
-(r2 + r3.*sin(th3guess) - r4.*sin(th4guess));...
-(rbc.*cos(th3guess) + r5guess.*cos(th5guess) - rbe.*cos(th4guess));...
-(rbc.*sin(th3guess) + r5guess.*sin(th5guess) - rbe.*sin(th4guess))];
% setting break criterion is iterations do not converge.
if iter > 100
break
end
end
% converged values of unknown variables are output as th3 for
% theta3, th4 for theta4, th5 for theta5,and r5
th3 = th3guess;
th4 = th4guess;
th5 = th5guess;
r5 = r5guess;
end

Answers (1)

Matt J
Matt J on 24 Feb 2022
Well, the equation parameters are not changing in your loop, it seems to me. NR() is just being given the same equation over and over again.
  16 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!