How to stop result being rounded - Symbolic mathematics

1 view (last 30 days)
Thank you so much for any assistance you can provide.
I am trying to find the time (t > 0) where a function is a maximum, and the integral of the function (0 < t < inf) using symbolic mathematics. Doing this same problem by hand, or using Wolfram-Alpha or using Maple, I get a solution of t = -0.000903 (which we disregard) and t = 0.00168. The value of the integral is 0.00297. With MATLAB, I get a solution of t = -9.3e-04 (which we disregard) and t = 0.00170. The value of the integral is 0.0030. I understand that the MATLAB solutions are merely a rounded version of the other answers, but I cannot seem to change the way the results are displayed to the command window to obtain the final solutions I expect of t = 0.00168 and the integral of 0.00297.
After reading through other forums, I have tried to use the vpa command (see below) and I have tried to alter the way the result is displayed on the command windown using fprintf.
syms t
%Describe the voltage and current functions
v=(10000*t+5)*exp(-400*t);
i=(40*t+0.05)*exp(-400*t);
%Obtain power
p=i*v;
%Obtain the derivative of the power and the time(s) when the derivative is zero
deriv_p=diff(p);
t_when_deriv_p_is_zero=solve(deriv_p == 0, t)
%Attempt to increase the number of digits displayed using vpa
t_when_deriv_p_is_zero=vpa(solve(deriv_p==0),6)
%Obtain only values of time when t>0
%Attempt to increase the number of digits displayed using vpa
t_of_max_p_1=vpa(t_when_deriv_p_is_zero(t_when_deriv_p_is_zero>0),6)
t_of_max_p=t_when_deriv_p_is_zero(t_when_deriv_p_is_zero>0)
%Attempt to increase the number of digits displayed using fprintf
fprintf('%1.5f\n', t_of_max_p_1)
fprintf('%1.5f\n', t_of_max_p)
%Obtain the total energy through integration of the power
%Attempt to increase the number of digits using vpa and fprintf
w_total=vpa(int(p,t,0,inf),6)
w_total=int(p,t,0,inf)
fprintf('%1.5f', w_total)
Thank you for helping me understand MATLAB a little better!!
  1 Comment
Torsten
Torsten on 11 Jan 2022
You mean something like
fprintf('%1.10f\n', double(t_of_max_p_1))
?

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 11 Jan 2022
No problems here.
syms t positive
%Describe the voltage and current functions
v=(10000*t+5)*exp(-400*t);
i=(40*t+0.05)*exp(-400*t);
%Obtain power
p=i*v;
%Obtain the derivative of the power and the time(s) when the derivative is zero
deriv_p=diff(p);
t_when_deriv_p_is_zero=solve(deriv_p == 0, t)
t_when_deriv_p_is_zero = 
%Attempt to increase the number of digits displayed using vpa
t_when_deriv_p_is_zero=vpa(solve(deriv_p==0),6)
t_when_deriv_p_is_zero = 
0.00168004
%Obtain only values of time when t>0
%Attempt to increase the number of digits displayed using vpa
t_of_max_p_1=vpa(t_when_deriv_p_is_zero(t_when_deriv_p_is_zero>0),6)
t_of_max_p_1 = 
0.00168004
t_of_max_p=t_when_deriv_p_is_zero(t_when_deriv_p_is_zero>0)
t_of_max_p = 
0.001680038313613796407963718593237
%Attempt to increase the number of digits displayed using fprintf
fprintf('%1.5f\n', t_of_max_p_1)
0.00168
fprintf('%1.5f\n', t_of_max_p)
0.00168
%Obtain the total energy through integration of the power
%Attempt to increase the number of digits using vpa and fprintf
w_total=vpa(int(p,t,0,inf),6)
w_total = 
0.00296875
w_total=int(p,t,0,inf)
w_total = 
fprintf('%1.5f', w_total)
0.00297
  11 Comments
Christine Zakzewski
Christine Zakzewski on 12 Jan 2022
I ended up deleting all the files from the preferences folder (except the history files), and that solved the issue as well. After deleting the preferences folder, I changed the sympref file back to default and now the program is working as expected, and the results are exact solutions. I assume this means there was something changed in the preferences folders (although I do not use MATLAB enough to go around messing with preferences intentionally!).

Sign in to comment.

More Answers (0)

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!