the error"The function values at the interval endpoints must differ in sign" + fzero

Hello. I want to solve the beneath nonlinear eqn(there is only one variable) as the code below. When using fzero, I get the error as"The function values at the interval endpoints must differ in sign.". I know there is a correct root at x=3.47 (which i am looking for).... Is my coding for fzero correct?
c=0
while Error>tol
c=c+1
syms x positive
for i=1:n+1
if i==1
EE(1,1)=0;
else
EE(i,1)=(((C*l(i-1)*x)+(((tan(phi*pi/180))*x)*((rho*9.81*A(i-1)*(cos(beta(i-1))))+(EE(i-1)*(sin(beta(i-1)-(te))))))-(rho*9.81*A(i-1)*(sin(beta(i-1))))+(EE(i-1)*(cos(beta(i-1)-(te)))))/((cos(beta(i-1)-(te)))+((sin(beta(i-1)-(te)))*(tan(phi*pi/180))*x)));
end
end
eqn = (simplify((EE(n+1,1))));
fh = @(x) (eqn);% you should use matlabFunction's 'vars' option to convert the variables into a vector.
if c==1
x0=[0 5];
else
x0=k
end
[x,fval,exitflag] =fzero(fh,x0),

4 件のコメント

x0 = 5
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2020 年 7 月 9 日
I replaced the range for x=[0 5] as you mentioned with x0=5 but I get the following error" Exiting fzero: aborting search for an interval containing a sign change because no sign change is detected during search. Function may not have a root.
x =
NaN
fval =
NaN
madhan ravi
madhan ravi 2020 年 7 月 9 日
Or 3?
Again, the same error.
As I could get the result from
[k] = vpasolve(eqn, x ,[0 5]);
Dont you think the function handle that i have created is wrong(i.e. fh = @(x) (eqn) )?

サインインしてコメントする。

回答 (1 件)

madhan ravi
madhan ravi 2020 年 7 月 9 日
fh = @(x) matlabFunction(eqn)

5 件のコメント

Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2020 年 7 月 9 日
編集済み: Pooneh Shah Malekpoor 2020 年 7 月 9 日
I changed the code as you mentioned:
fh = @(x) matlabFunction(eqn);
if c==1
x0=5;
else
x0=k
end
[x,fval,exitflag] =fzero([fh],{x0}),
Then I came across this error:"FZERO accepts inputs only of data type double."
madhan ravi
madhan ravi 2020 年 7 月 9 日
{x0} -> x0 why do you complicate things ? XD
fh = @(x) matlabFunction(eqn);
if c==1
x0=5;
else
x0=k
end
[x,fval,exitflag] = fzero(fh,x0);
Ok, then this error!
"Undefined operator '==' for input arguments of type 'function_handle'."
madhan ravi
madhan ravi 2020 年 7 月 9 日
What’s value of c?
Pooneh Shah Malekpoor
Pooneh Shah Malekpoor 2020 年 7 月 10 日
I prefered to use vpasolve as I got errors each time.
Thanks

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeOptimization についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by