MATLAB Answers

I'm getting very long fractions in my solution rather than decimals. All of my system preferences are standard. Below is the code

17 views (last 30 days)
Caleb Hedin
Caleb Hedin on 13 Nov 2020
Answered: Walter Roberson on 13 Nov 2020
clear all
syms h e R_a R_p;
R_E = 6375*1000; %m Radius of the Earth
R1 = (2500000+R_E); %Radius of true anomaly 1
R2 = (1000000+R_E); %radius of true anomaly 2
G = 6.67*10^(-11); %G constant m^3/(kg)*s^s
M = 5.972*10^(24); %mass of Earth in Kilograms
u = G*M;
equation1 = (h^2)/u*(1/(1+e*cosd(130))) == R1;
equation2 = (h^2)/u*(1/(1+e*cosd(55))) == R2;
[h,e] = solve([equation1,equation2,h>0,e>0],[h,e]);
a = (h^2/u)*(1/(1-e^2));
equation3 = (R_a+R_p)/2 == a;
equation4 = (R_a-R_p)/(R_a+R_p) == e;
[R_a,R_p] = solve([equation3,equation4,R_a>0,R_p>0],[R_a,R_p]);
elevation_p = (R_p-R_E)/1000
Period = 2*pi*sqrt((a^3)/u)/60/60
Caleb Hedin
Caleb Hedin on 13 Nov 2020
Oh I see, vpa is another display function. Thank you Geoff.
Is it displaying as a fraction because of the matrices?

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 13 Nov 2020
It is displaying as a fraction because you used solve() and solve() is intended to find the indefinitely precise solution whenever it can.
Using solve is never appropriate when you have coefficients that are floating point. For example 6.67*10^(-11) is not an exact value: the exact value is somewhere between 6.665E-11 and 6.675E-11 (the recommended value is 6.67430 plus some uncertainty.) It makes no logical sense to ask for the exact solution to inputs that are probably wrong.
If you have floating point numbers that are short-hand for numbers known exactly (such as 0.5 when the number is known to be exactly 1/2) then replace the floating point values with the symbolic form of the exact value, such as sym(1)/sym(2) .
If you have any floating point numbers with any uncertainty, then either code the uncertainty as an explicit symbol, or else do not use solve().
The function to use for numeric approximation of symbolic equations is vpasolve() instead of solve()


Community Treasure Hunt

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

Start Hunting!