Solving an Equation in Matlab

I am trying to solve the following for t:
(16.68t) - ((16.68 - 0)*(1-(exp(-0.21t)))/0.21) - (8.5) = 0

4 Comments

help fzero
FZERO Single-variable nonlinear zero finding. X = FZERO(FUN,X0) tries to find a zero of the function FUN near X0, if X0 is a scalar. It first finds an interval containing X0 where the function values of the interval endpoints differ in sign, then searches that interval for a zero. FUN is a function handle. FUN accepts real scalar input X and returns a real scalar function value F, evaluated at X. The value X returned by FZERO is near a point where FUN changes sign (if FUN is continuous), or NaN if the search fails. X = FZERO(FUN,X0), where X0 is a vector of length 2, assumes X0 is a finite interval where the sign of FUN(X0(1)) differs from the sign of FUN(X0(2)). An error occurs if this is not true. Calling FZERO with a finite interval guarantees FZERO will return a value near a point where FUN changes sign. X = FZERO(FUN,X0), where X0 is a scalar value, uses X0 as a starting guess. FZERO looks for an interval containing a sign change for FUN and containing X0. If no such interval is found, NaN is returned. In this case, the search terminates when the search interval is expanded until an Inf, NaN, or complex value is found. Note: if the option FunValCheck is 'on', then an error will occur if an NaN or complex value is found. X = FZERO(FUN,X0,OPTIONS) solves the equation with the default optimization parameters replaced by values in the structure OPTIONS, an argument created with the OPTIMSET function. See OPTIMSET for details. Used options are Display, TolX, FunValCheck, OutputFcn, and PlotFcns. X = FZERO(PROBLEM) finds the zero of a function defined in PROBLEM. PROBLEM is a structure with the function FUN in PROBLEM.objective, the start point in PROBLEM.x0, the options structure in PROBLEM.options, and solver name 'fzero' in PROBLEM.solver. [X,FVAL]= FZERO(FUN,...) returns the value of the function described in FUN, at X. [X,FVAL,EXITFLAG] = FZERO(...) returns an EXITFLAG that describes the exit condition. Possible values of EXITFLAG and the corresponding exit conditions are 1 FZERO found a zero X. -1 Algorithm terminated by output function. -3 NaN or Inf function value encountered during search for an interval containing a sign change. -4 Complex function value encountered during search for an interval containing a sign change. -5 FZERO may have converged to a singular point. -6 FZERO can not detect a change in sign of the function. [X,FVAL,EXITFLAG,OUTPUT] = FZERO(...) returns a structure OUTPUT with the number of function evaluations in OUTPUT.funcCount, the algorithm name in OUTPUT.algorithm, the number of iterations to find an interval (if needed) in OUTPUT.intervaliterations, the number of zero-finding iterations in OUTPUT.iterations, and the exit message in OUTPUT.message. Examples FUN can be specified using @: X = fzero(@sin,3) returns pi. X = fzero(@sin,3,optimset('Display','iter')) returns pi, uses the default tolerance and displays iteration information. FUN can be an anonymous function: X = fzero(@(x) sin(3*x),2) FUN can be a parameterized function. Use an anonymous function to capture the problem-dependent parameters: myfun = @(x,c) cos(c*x); % The parameterized function. c = 2; % The parameter. X = fzero(@(x) myfun(x,c),0.1) Limitations X = fzero(@(x) abs(x)+1, 1) returns NaN since this function does not change sign anywhere on the real axis (and does not have a zero as well). X = fzero(@tan,2) returns X near 1.5708 because the discontinuity of this function near the point X gives the appearance (numerically) that the function changes sign at X. See also ROOTS, FMINBND, FUNCTION_HANDLE. Documentation for fzero doc fzero Other uses of fzero optim/fzero
Unless, of course, this is just your homework. In which case you would have been directed not to use fzero. But then you would need to be doing the work yourself anyway.
Hi. Thank you. Not for homework.
I used the following:
t = fzero(@(t)16.68*t - (16.68*(1-(exp(-0.21*t)))/0.21) - 110.85,0.1)
**[I used 110.85 instead of 8.5 in the original posted question]
However, the t value being found is negative . A positive t value of 10.93 will make the equation zero. I would like the positive value since it is a time parameter.
t = fzero(@(t)16.68*t - (16.68*(1-(exp(-0.21*t)))/0.21) - 110.85,10)
t = 10.9277
fzero will find the nearest root; change the starting value...
Thank you. This worked!!

Sign in to comment.

Answers (1)

Sounds like you got an answer that worked, but let me expand. You have a function:
f = @(t)16.68*t - (16.68*(1-(exp(-0.21*t)))/0.21) - 110.85;
And you need to find a zero, that is, a value of t such that f(t)==0.
First, ALWAYS PLOT EVERYTHING. Then, find something more to plot, if plotting everything was not sufficient. And when done, think about what you see. Does what you see make sense?
fplot(f)
So no solution in the interval [-5,5], the default for fplot.
fplot(f,[-20,20])
grid on
And that tells us there will be two roots. A negative one, and a positive one. One will be near -5, and the other near +10. Well, at LEAST two roots. If we looked further out, we may find more.
If you give fzero only ONE starting value, then it tries to find a root, but it won't really care which one it finds. A root is a root for god sakes! You asked for a root. ;-) For example:
[xsol,fval] = fzero(f,0)
xsol = -6.2371
fval = 1.4211e-14
You can see it decided to find the negative root, but that is just the one it found.
If you want to find the positive one, then use a bracket for fzero that is positive. A bracket is a pair of values for x, such that the function is positive at one end, and negative at the other.
[xsol,fval] = fzero(f,[0,1000])
xsol = 10.9277
fval = 2.8422e-14
This will insure that fzero always finds the positive root.

Categories

Find more on Optimization in Help Center and File Exchange

Tags

Asked:

on 15 Oct 2022

Edited:

on 15 Oct 2022

Community Treasure Hunt

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

Start Hunting!