MATLAB Answers

# Outrageous values when printing out a function

3 ビュー (過去 30 日間)
seldiora 2019 年 4 月 11 日

I'm trying to print out a function with defined variables,
vcx = sol(1)*exp(root0*x)+ sol(2)*exp(root1*x);
when testing, I try
fprintf( "%de ^(%d t) + %de ^(%d t)\n",sol(1), root0, sol(2), root1);
the result is
7e ^(-1.866025e+00 t) + -2e ^(-1.339746e-01 t)
these are the values i expected -- 7 for sol(1), -1.866... for root0, -2 for sol(2), and -0.13397... for root1.
However, fprintf("%s", vcx) obtained
(222119821791395497*exp(-(8403831313147477*t)/4503599627370496))/31201853486215849 - (66110554360316252*exp(-(2413471766374059*t)/18014398509481984))/31201853486215849
why is this? This doesn't make any sense. I double checked and root0 and root1 didn't change at least, so what's up with this insane print out?
notice:
I tried solve(vct,x) but this didn't work because the vct isn't an equation.
I also tried solx = solve(solution,x)
where solution = vct == sol(1)*exp(root0*x)+ sol(2)*exp(root1*x);
but this didn't work either.
(solution became 0)

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

### 回答 (3 件)

Mark Sherstan 2019 年 4 月 11 日

Try solving using symbolics and then simplify the expression and see if the error repeats. This would also be a good use for MATLAB's live editor. It appears that MATLAB is simplifying your answer in terms of integers instead of floats.
##### 0 件のコメント表示非表示 -1 件の古いコメント

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

seldiora 2019 年 4 月 12 日
I tried solve(vct,x) but this didn't work because the vct isn't an equation.
I also tried solx = solve(solution,x)
where solution = vct == sol(1)*exp(root0*x)+ sol(2)*exp(root1*x);
but this didn't work either.
##### 0 件のコメント表示非表示 -1 件の古いコメント

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

Walter Roberson 2019 年 4 月 12 日
vpa(vcx)

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

R2017a

### Community Treasure Hunt

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

Start Hunting!