Unable to obtain double precision from symbolic value

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 %%
radian_to_deg = 180/pi;
degree_to_radian = pi/180;
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)))

 채택된 답변

You need the vpa function
x = 2;
a = sym(1/3);
f = a*sin(2*pi*x)
f = 
fVpa = vpa(f,10)
fVpa = 

댓글 수: 3

x(1,1) = vpa(-ya/tand(theta(1,1)-mach_angle(1,1)),4);
Thank you!. It does work now. However, x(1,1) does not get rounded off to 4 siginificant digits. Any reason behind this?
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
Thank you!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Ayush Modi
Ayush Modi 2024년 7월 18일
편집: Ayush Modi 2024년 7월 18일
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:

댓글 수: 2

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
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

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

제품

릴리스

R2024a

질문:

2024년 7월 18일

댓글:

2024년 7월 18일

Community Treasure Hunt

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

Start Hunting!

Translated by