# Unable to obtain double precision from symbolic value

6 views (last 30 days)
Bamelari Jovani on 18 Jul 2024
Commented: Bamelari Jovani on 18 Jul 2024
Hello everyone,
MATLAB returns a symbolic value for x(1,1). How do I go about converting it into a double precision?
Thank you!
clear;
clc;
close all;
%% Initial parameters %%
prompt = "Enter a Mach Number ranging from 1.5 to 4: ";
Me = input(prompt);
prompt_1 = "Enter the number of divisions: ";
n_div = input(prompt_1);
prompt_2 = "Enter the throat radius: ";
TR = input(prompt_2);
g = 1.4;
mach_angle= [];
theta = [];
theta_a = [];
y=[];
x=[];
v=[];
v_a=[];
%% get mach angle
ma_f = @(x) asind(1/x) ;
%x = Me;
%ma_angle = ma_f(x);
%% get theta_max
A = sqrt((g+1)/(g-1));
B = (g-1)/(g+1);
pm_f = @(x) (A*atand(sqrt(B*(x^2-1))) - atand(sqrt(x^2-1)));
theta_max = pm_f(Me)/2;
%% extraction of fractional part of theta_max
%dT = theta_max/n_div;
%theta(:,1) = (dT:dT:theta_max);
theta_i = sign(theta_max)*(abs(theta_max) - floor(abs(theta_max)));
%{
for i = 2:n_div
theta(i,1) = theta(i-1,1) + 3.0;
end
%}
%% Storing different values of theta_a
%point a
for i = 1:n_div
theta_a(i,1) = theta_i ;
theta_i = theta_i + 3.0;
end
v_a = theta_a;
%% Getting properties of a and 1
ya=1;
xa=0;
%theta_a(1,1) = theta_i(1,1);
v_a(1,1) = theta_a(1,1);
%point 1
y(1,1) = 0;
Km_1 = theta_a(1,1) + v_a(1,1);
theta(1,1)= 0.5*(Km_1);
v(1,1) = 0.5*(Km_1);
%% get mach number for v(1,1)
syms x
f = @(x) (A*atand(sqrt(B*(x^2-1))) - atand(sqrt(x^2-1))-v(1,1));
f1 = matlabFunction(diff(f(x)));
%NR Method using a while loop
change = 10;
Mguess = 1.1;
while (change>1e-6)
Mnew = Mguess - f(Mguess)/f1(Mguess);
change = abs(Mguess - Mnew);
Mguess = Mnew;
end
mach_angle(1,1) = ma_f(Mnew);
x(1,1) = (-ya/tand(theta(1,1)-mach_angle(1,1)))

Aquatris on 18 Jul 2024
You need the vpa function
x = 2;
a = sym(1/3);
f = a*sin(2*pi*x)
f =
fVpa = vpa(f,10)
fVpa =
##### 3 CommentsShow 1 older commentHide 1 older comment
Aquatris on 18 Jul 2024
Edited: Aquatris on 18 Jul 2024
Because vpa uses "at least X digits" where X is 4 in your case. So it does not round of if it can do more than X digits. If you want to round off you should use round function with the output of the vpa function.
x = 0.95472222;
x_rounded = round(x,4)
x_rounded = 0.9547
x_rounded-x
ans = -2.2220e-05
Bamelari Jovani on 18 Jul 2024
Thank you!

Ayush Modi on 18 Jul 2024
Edited: Ayush Modi on 18 Jul 2024
Hi Bamelari,
You can convert the symbolic value into double precision value using double method. Here is a sample code to demonstrate this:
syms a b x
a = sym(3);
b = sym(2);
x = a / b;
disp(x); % This will display 3/2 (symbolic form)
% Convert the symbolic result to double precision
x_double = double(x);
disp(x_double);
1.5000
For more information on double method, refer to the following MathWorks documentation:
Bamelari Jovani on 18 Jul 2024
x(1,1) = double(-ya/tand(theta(1,1)-mach_angle(1,1)));
I did try this. MATLAB still returns it in symbolic form.
Output of the above statement is:
1346076948282197/4503599627370496
Ayush Modi on 18 Jul 2024
You need to convert the symbolic variable to double. Right now, even though you are using double, you are still assigning the value to symbolic variable.
To get the final value in double precision, modify the code as below -
x(1,1) = -ya/tand(theta(1,1)-mach_angle(1,1));
double_x = double(x(1,1));
% Note that double_x is a new variable

### Categories

Find more on Assumptions in Help Center and File Exchange

R2024a

### Community Treasure Hunt

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

Start Hunting!