Borrar filtros
Borrar filtros

Index exceeds the number of array elements error when using fsolve

3 visualizaciones (últimos 30 días)
I'm working on a project in which one part solves for the position solutions from a vector loop equation I generated. The code reads as follows:
%%position solution
%VLE: -R1 - R2 + R3 = 0
%defining fixed/knowns (m) - using average values of males (Table 2)
R1 = 0.4; %length of thigh
R2 = 0.4; %length of shin
th3 = 90*(pi/180); %converted to radians
%using fsolve for position solution *requires installation of optimization toolbox in MatLab
PAs = zeros(141,3); %empty array for th1 and R3, respectively, (unknowns) to be placed
for ii = 0:140 %to iteart through all possible input angles (degrees)
theta2 = ii*(pi/180); %converst to radians
PAs(ii+1,1) = theta2; %fills first spot in array with input value (th2) - knee angle of flexion
F1 = @(x) [-R1*cos(x(2)*(pi/180))-R2*cos(theta2); -R1*sin(x(2)*(pi/180))-R2*sin(theta2)+x(3)];
PAs(ii+1, [2 3]) = fsolve(F1, [0, 0.8]); %initial values (taken from standing leg)
end
th2 = PAs(:,1);
th1 = PAs(:,2);
r3 = PAs(:,3);
However, when I run it, I get an error reading "index exceeds number of array elements" and thus fsolve will not compute. Is anyone able to see any issues with my code that would cause this error?

Respuesta aceptada

Stephen23
Stephen23 el 26 de Mzo. de 2024
Movida: Matt J el 26 de Mzo. de 2024
Your x0 is a two-element vector, so you told FSOLVE to call the function handle with a two-element vector. But then inside the function you try to access x(3), which thus does not exist.
If you expect the function handle to be called with a three-element vector, then x0 needs to have three elements.

Más respuestas (0)

Etiquetas

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by