Solving Exponencial fuction is not returning the right answer

1 回表示 (過去 30 日間)
Rodrigo Toledo
Rodrigo Toledo 2021 年 4 月 2 日
コメント済み: William Rose 2021 年 4 月 4 日
How do i solve this exponencial:
12734.40 == 12000 * 1.02^x
i am doing:
solve(12734.4 == 12000*1.02^x,x
and getting log(2653/2500) / log(51/50)
the answer shoud be a 3.

採用された回答

John D'Errico
John D'Errico 2021 年 4 月 2 日
編集済み: John D'Errico 2021 年 4 月 2 日
Yes, you THINK the true answer is 3. But it is not.
format long g
log(2653/2500) / log(51/50)
ans =
2.99961931278214
And that is effectively the exact answer (to 16 significant digits) to the problem you posed. You could have made MATLAB convert that number to a floating point number, using either vpa or double.
What would the true left hand side have been, if 3 had been the correct result?
12000*sym('1.02')^3
ans = 
12734.496
So you had 12734.40 in the equation you tried to solve. Should MATLAB have solved a different problem than the one you posed? Surely not! MATLAB cannot know that you only had approximate coefficients, and that you expected an integer result. At least not unless you tell it to look for an approximate solution using some more appropriate solution method.
For example, if I use a solver that can constrain the unknown to be an integer, minimizing the absolute error, I get this:
fun = @(x) 12734.40 - 12000*1.02.^x;
lb = 0;
[xval,fval,exitflag] = ga(@(x) abs(fun(x)),1,[],[],[],[],lb,[],[],1)
Optimization terminated: average change in the penalty fitness value less than options.FunctionTolerance and constraint violation is less than options.ConstraintTolerance.
xval =
3
fval =
0.0960000000013679
exitflag =
1
Here MATLAB was able to find an integer solution.
  2 件のコメント
Rodrigo Toledo
Rodrigo Toledo 2021 年 4 月 2 日
@William Rose @John D'Errico Thanks for the answers and to explain to me that what i was assuming is wrong. Thanks guys. Its much clear to me now. xD
William Rose
William Rose 2021 年 4 月 3 日
You're elcome Rodrigo.

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

その他の回答 (1 件)

William Rose
William Rose 2021 年 4 月 2 日
Algebra:
x=log(12734/12000)/log(1.02)
>> x=log(12734/12000)/log(1.02)
x =
2.9980
>>
  4 件のコメント
Rodrigo Toledo
Rodrigo Toledo 2021 年 4 月 3 日
another issue i am facing with matlab: trying to get x=2 and y=3 for this equation:
@syms x y
eq = x^2 + y^3 = 31
solve(eq)
any tips?
thx
William Rose
William Rose 2021 年 4 月 4 日
@Rodrigo Toledo, I am not familiar with Matlab's symbolic math routines. I don't know if you can instruct Matlab to restrict the solution space to (or restrict it to ).

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

カテゴリ

Help Center および File ExchangeLinear Algebra についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by