Why am I getting the not enough inputs error on line 63 when defining matrix (f=)?

1 Ansicht (letzte 30 Tage)
% 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 R2, R3, and R4 in inches
r2 = 55.18;
r3 = 27.59;
r4 = 27.59;
% 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
th3guess = 50;
th4guess = 50;
th5guess = 35;
r5guess = 45.18;
% 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,r2,r3,r4,r5guess);
% 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
% Ending timer
toc
function [th3,th4,th5,r5] = NR(~,~,~,th3guess,th4guess,th5guess,r2,r3,r4,r5guess)
% 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 = [-(r2 + r3.*cos(th3guess) - r4.*cos(th4guess));...
-(0 + r3.*sin(th3guess) - r4.*sin(th4guess));...
-(r3.*cos(th3guess) + r5guess - r4.*cos(th4guess));... %<== where the error occurs
-(r3.*sin(th3guess) + 0 - r4.*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;...
r3.*sin(th3guess),r4.*sin(th4guess),-r5guess.*sin(th5guess),cos(th5guess);...
-r3.*cos(th3guess),-r4.*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 = [-(r2 + r3.*cos(th3guess) - r4.*cos(th4guess));...
-(0 + r3.*sin(th3guess) - r4.*sin(th4guess));...
-(r3.*cos(th3guess) + r5guess - r4.*cos(th4guess));...
-(r3.*sin(th3guess) + 0 - r4.*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

Akzeptierte Antwort

Torsten
Torsten am 20 Feb. 2022
function [th3,th4,th5,r5] = NR(~,~,th3guess,th4guess,th5guess,r2,r3,r4,r5guess)
instead of
function [th3,th4,th5,r5] = NR(~,~,~,th3guess,th4guess,th5guess,r2,r3,r4,r5guess)
  7 Kommentare
Torsten
Torsten am 21 Feb. 2022
How can the Jacobian depend on th5 if th5 does not appear in the function definition for f ?
Also check the Jacobian - I think some partials are wrong.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by