Clear Filters
Clear Filters

Using fsolve to solve nonlinear equations that have user-defined functions

2 views (last 30 days)
Hi there,
I have a problem using fsolve to solve nonlinear equations that have my own functions. See the code below:
options = optimset('TolFun', 1e-10, 'Display', 'iter', 'MaxFunEvals', 1e10, 'MaxIter', 1e9, 'Algorithm', 'Levenberg-Marquardt');
w=2;
h=1;
l=20;
n=0;
M=0;
E=70*10^9;
I= 1/12*h^3*w;
L=l;
UnknownGuess = rand(1,1);
[Unknowns, fval,exitflag] = fsolve('kkk', UnknownGuess, options,n,M,E,I,L);
P = Unknowns(1);
%%%%%%%%%%%%
function F = kkk(Unknown,n,M,E,I,L)
F(1)=XX([n,Unknown(1),E,I,M,L])+2;
end
%%%%%%%%%%%%
function Y=XX(X)
n=X(1,1);
P=X(1,2);
E=X(1,3);
I=X(1,4);
M=X(1,5);
L=X(1,6);
xmesh = linspace(0,L,100);
solinit = bvpinit(xmesh, @guess);
sol = bvp4c(@bvpfcn, @bcfcn, solinit);
fitobject = fit(sol.x(1,:)',sol.y(1,:)','poly9');
fun1 = @(x) cos(fitobject.p1.*x.^9 + fitobject.p2.*x.^8 + fitobject.p3.*x.^7 + fitobject.p4.*x.^6 + fitobject.p5.*x.^5 + fitobject.p6.*x.^4 + fitobject.p7.*x.^3 + fitobject.p8.*x.^2 + fitobject.p9.*x + fitobject.p10);
Y=integral(fun1,0,L)-L;
function g = guess(x)
g = [sin(x)
cos(x)];
end
function dydx = bvpfcn(x,y)
dydx = [y(2)
-P/E/I*(cos(y(1))+n*sin(y(1)))];
end
function res = bcfcn(ya,yb)
res = [ya(1)
yb(2)-M/(E*I)];
end
end
%%%%%%%%%%%%%
First-Order Norm of
Iteration Func-count Residual optimality Lambda step
0 2 4 0 0.01
No solution found.
fsolve stopped because the problem appears regular as measured by the gradient,
but the vector of function values is not near zero as measured by the
selected value of the function tolerance.
<stopping criteria details>
I have checked the conditions of fsolve, such as: the equation to be solved has to be continuous.etc..however, they all match.
I think I must have missed something or made a mistake somewhere in my script as 'kkk' along with the given conditions does have a solution:
XX([n,4.16*10^7,E,I,M,L]) is around -2
Can anyone help me?
Thanks in advance,
Ke
  3 Comments
Alan Stevens
Alan Stevens on 17 Feb 2021
fsolve requires the optimisation toolbox, which I don't have, so I'm no use to you here I'm afraid!

Sign in to comment.

Answers (0)

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!