Nonlinear equation solver issue

1 view (last 30 days)
Michael ODonnell
Michael ODonnell on 29 Apr 2020
Commented: Walter Roberson on 1 Aug 2020
I am trying to solve a system of nonlinear equations for two variables, in which some of the variables in the equations are vectors. When I run fsolve, I get the following error:
My function is defined as:
function F = root2d(x,m,g,L0,Kleg,u,T)
K_vert = x(1);
v = x(2);
F(:,1) = T - ((4.*sqrt(m./x(1))).*(pi-atan((x(2)./g).*sqrt(x(1)./m)))+(4.*x(2)./g));
F(:,2) = (x(1)./Kleg) - (1 + ((L0.*x(1)-sqrt(L0.^2.*x(1).^2-m.*x(1).*u.^2.*(pi-atan((x(2)./g).*sqrt(x(1)./m))).^2))./(m.*g+sqrt(m.^2.*g.^2+m.*x(1).*x(2).^2))));
end
Where the original equations are:
And, my code is:
g = 9.81;
m = 90.72;
uL = 0.364; % length of upper leg (m)
lL = 0.543; % length of lower leg (m)
L0 = uL+lL;
K_leg1 = 15;
K_leg2 = 0.715*m^0.67;
K_leg = (K_leg1+K_leg2)/2;
u = linspace(1.42,2.7,150);
f = 0.65 + 0.2.*u;
T = 1./f;
x0 = [18;3]';
for i = 1:150
x = fsolve(@(x)root2d(x,m,g,L0,K_leg,u(i),T(i)),x0);
K_vert(i) = x(1);
v(i) = x(2);
end
The value for K_vert has to be greater than 15, and the value for v should be in between 0 and 5, so I know that my starting values should not be the cause.
Any help would be appreciated! I've been trying to figure this out for hours.
  2 Comments
Walter Roberson
Walter Roberson on 29 Apr 2020
Volunteers have an easier time answering when they are able to copy and paste code instead of having to type it in from a picture of the code.
Michael ODonnell
Michael ODonnell on 29 Apr 2020
Sorry, this is my first time posting. It is fixed now

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 29 Apr 2020
With those constants, solutions appear to be imaginary. The starting point becomes important.
g = 9.81;
m = 90.72;
uL = 0.364; % length of upper leg (m)
lL = 0.543; % length of lower leg (m)
L0 = uL+lL;
K_leg1 = 15;
K_leg2 = 0.715*m^0.67;
K_leg = (K_leg1+K_leg2)/2;
N = 150;
u = linspace(1.42,2.7,N);
f = 0.65 + 0.2.*u;
T = 1./f;
%x0 = [18;3]';
x0 = [10-1i, -1-1i];
K_vert = zeros(1,N);
v = zeros(1,N);
fval = zeros(N,2);
options = optimoptions('fsolve','Display','none');
for i = 1:N
[x, fval(i,:)] = fsolve(@(x)root2d(x,m,g,L0,K_leg,u(i),T(i)), x0, options);
K_vert(i) = x(1);
v(i) = x(2);
x0 = x;
end
Question: is your m^0.67 intended to represent m^(2/3) ?

Mohamed Amin Ibrahim
Mohamed Amin Ibrahim on 1 Aug 2020
Edited: Walter Roberson on 1 Aug 2020
function ret=Code(lenchrom,bound)
%本函数将变量编码成染色体,用于随机初始化一个种群
% lenchrom input : 染色体长度
% bound input : 变量的取值范围
% ret output: 染色体的编码值
flag=0;
while flag==0
pick=rand(1,length(lenchrom));
ret=bound(:,1)'+(bound(:,2)-bound(:,1))'.*pick; %线性插值
flag=test(lenchrom,bound,ret); %检验染色体的可行性
end
  2 Comments
Mohamed Amin Ibrahim
Mohamed Amin Ibrahim on 1 Aug 2020
hello am new to matlab but when i try to run this code i usually got error, please show me where there is error
Walter Roberson
Walter Roberson on 1 Aug 2020
What error message are you seeing?
MATLAB does not have a function named test so unless you have your own function test() then test(lenchrom,bound,ret) will fail.

Sign in to comment.

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!