Finding a root with interval constraint

Hello there!
I am trying to find a point x within the time interval [t-1,t] (for some t, say t = 3) so that the function attains value zero. That is, I want to solve "Q_0 + integral(a+b*sin(c*t+d)-mu,t-1,x) = 0" for x in [t-1,t]. My code is the following
y = fsolve(@(x) Q_0+(a-mu)*(x-t+1)-(b/c)*cos(c*x+d)+(b/c)*cos(c*(t-1)+d),0,optimset('Display','off'))
wherein (a,b,c,d) satisfy a + b*sin(c*t+d), and Q_0 and mu are constants. This code has no problem. However, the solution may sometimes be outside the time interval [t-1,t], which is not what I want.
So, my question is if there is a way to restrict the routine to find a solution that lies within [t-1,t] exactly?
Thanks!

 Respuesta aceptada

Walter Roberson
Walter Roberson el 24 de En. de 2014

0 votos

As your x0 is a scalar (0), your x are scalar, and that implies you can use fzero() instead of fsolve(). With fzero() you can pass the interval [t-1 t] as your x0.

5 comentarios

Chien-Chia Huang
Chien-Chia Huang el 24 de En. de 2014
It works! Thanks Walter.
Chien-Chia Huang
Chien-Chia Huang el 24 de En. de 2014
Another follow-up question:
How can I make my program remain running even if I cannot find a solution in the above nonlinear equation? As the value of mu changes, there might not have a solution in [t-1,t]. Is there any way to keep it going without stopping at error msg?
Thanks!
Walter Roberson
Walter Roberson el 24 de En. de 2014
If no root is found, fzero() will not throw an error. If you use the optional output arguments then by examining the exitflag argument you can detect whether an error was encountered: the exitflag will be negative.
Chien-Chia Huang
Chien-Chia Huang el 25 de En. de 2014
Editada: Chien-Chia Huang el 25 de En. de 2014
Thanks, Walter. My code now goes like this (value of mu changes)
[FirstVanish,~,exitflag] = fzero(@(x) queuelength(counter_qln)+(a(j)-mu)*(x-t+1)-(b(j)/c(j))*cos(c(j)*x+d(j))+(b(j)/c(j))*cos(c(j)*(t-1)+d(j)),[t-1,t])
However, it showed the error msg
Error using fzero (line 274) The function values at the interval endpoints must differ in sign.
This is why I will need to know how to get things going without seeing the above.
Walter Roberson
Walter Roberson el 25 de En. de 2014
Ah, then use try/catch

Iniciar sesión para comentar.

Más respuestas (0)

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by