Clear Filters
Clear Filters

Solving an Equation in Matlab

2 views (last 30 days)
Sean Sarran
Sean Sarran on 15 Oct 2022
Edited: John D'Errico on 15 Oct 2022
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
dpb
dpb on 15 Oct 2022
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...
Sean Sarran
Sean Sarran on 15 Oct 2022
Thank you. This worked!!

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 15 Oct 2022
Edited: John D'Errico on 15 Oct 2022
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.

Tags

Community Treasure Hunt

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

Start Hunting!