I am using Matlab to find the roots of a non-linear function. The equation is lengthy and I have used another .m
to save the function, the code for which goes like
function x_c = f_x_c(s,H,VA,Lo,qc,EA,NF,Sj,Fj)
if (s < 0) || (s > Lo);
disp('The value of s is invalid')
disp(['s = ' num2str(s)]);
return
end
C1 = H/qc;
if NF == 0
n = 0;
sn = 0;
sum_Fj = 0;
end
if NF >= 1
Sj_Q = [0; Sj; Lo];
%Determine n and sn if 0 <= s < Lo:
if s < Lo
STOP = 0;
k = 0;
while STOP == 0
k = k + 1;
if (s >= Sj_Q(k,1)) && (s < Sj_Q((k + 1),1))
STOP = 1;
end
end
n = k - 1;
sn = Sj_Q(k,1);
end
%Determine n and sn if s = Lo:
if s == Lo
n = NF;
sn = Sj(NF,1);
end
sum_Fj = sum(Fj(1:n,1));
end
x_c = (H/EA)*s;
x_c = x_c + C1*asinh((qc*s - VA + sum_Fj)/H) + ...
- C1*asinh((qc*sn - VA + sum_Fj)/H);
for j = 1:n
sk = Sj_Q((j + 1),1);
sk_1 = Sj_Q(j,1);
sum_Fj = sum(Fj(1:(j - 1)));
x_c = x_c + ...
+ C1*asinh((qc*sk - VA + sum_Fj)/H) + ...
- C1*asinh((qc*sk_1 - VA + sum_Fj)/H);
end
The variable is H
here. There is no problem with the code because it returns me that lengthy equation when I type the following in the main file.
syms x
equation = -(XB - XA) + f_x_c(s,x,VA,Lo,qc,EA,NF,Sj,Fj); %Replaced H with variable H and all other arguments are pre-defined
Now, I want to solve this equation near H0
. When I put fzero(@(x)equation, H0)
, it gives me an error which goes like
Undefined function 'isfinite' for input arguments of type 'sym'.
Error in fzero (line 308)
elseif ~isfinite(fx) || ~isreal(fx)
Error in main (line 50)
fzero(@(x)equation, H0)
How can I solve this problem?
EDIT:
The equation has at least one root because if I use ezplot
to plot the function, I get the following figure.