Simplifying output in Matlab

If you get an expression like this as output
syms x
fx = -(1.0*(1.82e+39*x^2 + 1.81e+32*x - 3.04e+39))/(6.35e+41*x + 4.27e+37)
How do you simplify the output and use smaller numbers, for example
fx = -(1.0*(1.82e+7*x^2 + 1.81*x - 3.04e+7))/(6.35e+9*x + 4.27e+5)

6 Comments

Likely the best to be hoped for is —
syms x
fx = -(1.0*(1.82e+39*x^2 + 1.81e+32*x - 3.04e+39))/(6.35e+41*x + 4.27e+37)
fx = 
fx = vpa(fx, 3)
fx = 
fx = vpa(simplifyFraction(fx), 3)
fx = 
.
Thank you for you help
Is the rule that you want the numerator coefficient of x^1 to be in the range [1, 10) ?
No, I wanted the output simplified because I was using ode45 to plot the function and initially when using the original expression the computation time was very large. For example, in the code below the plot takes a long time to generate.
tspan = [0 10];
x0 = 0;
[t,x] = ode45(@(t,x) -(1.0*(6.84e+45*x^2 + 5.24e+32*x - 2.49e+42))/(2.47e+39*x + 7.12e+37), tspan, x0);
plot(t,x,'b')
I was looking for a way to decrease the time, like simplifying it using smaller numbers although that doesn't always work.
The integration proceeded very quickly, running it in Input argument for ode45 function type error (running R2021b), however the coefficient magnitudes are large, although not so different between them that this could be regarded as a ‘stiff’ system. It could be worth experimenting with ode15s or other stiff solvers to see if the speed increases significantly.
The computation of smaller numbers is easier for human, but does not change the speed when performing the calculations on a computer.

Sign in to comment.

 Accepted Answer

The magnitude of the variables does not matter. For a computer, the addition of pi + 1.23456789, pi + 1.0 and pi+ 1.23456789e37 takes exactly the same time. Therefore the simplification does not accelerate the evaluation. And by the way, this is very fast in Matlab at all:
x = rand;
tic;
for k = 1:1e9
fx = -(1.0*(1.82e+39*x^2 + 1.81e+32*x - 3.04e+39))/(6.35e+41*x + 4.27e+37);
end
toc
Elapsed time is 0.777027 seconds.
tic;
for k = 1:1e9
fx = -(1.0*(1.82e+7*x^2 + 1.81*x - 3.04e+7))/(6.35e+9*x + 4.27e+5);
end
toc
Elapsed time is 0.706243 seconds.
Both take about 0.4 seconds on my i5-mobile Matlab R2018b. A tiny acceleration is measurable, if you omit the meaningless multiplication by 1.0.
The slow computation has another cause: ODE45 is a solver for non-stiff equations. Use a stiff solver for this equation instead:
tic
tspan = [0 10];
x0 = 0;
[t,x] = ode23s(@(t,x) -(1.0*(6.84e+45*x^2 + 5.24e+32*x - 2.49e+42))/(2.47e+39*x + 7.12e+37), tspan, x0);
plot(t,x,'b');
toc
Elapsed time is 0.207868 seconds.
There is a very sharp knee at the start. ODE45 struggles massively with it, because the stepsize controller drives crazy.

More Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Asked:

on 7 Nov 2021

Answered:

Jan
on 7 Nov 2021

Community Treasure Hunt

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

Start Hunting!