Unable to obtain double precision from symbolic value

2 次查看(过去 30 天)
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)))

采纳的回答

Aquatris
Aquatris 2024-7-18
You need the vpa function
x = 2;
a = sym(1/3);
f = a*sin(2*pi*x)
f = 
fVpa = vpa(f,10)
fVpa = 
  3 个评论
Aquatris
Aquatris 2024-7-18
编辑:Aquatris 2024-7-18
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

请先登录,再进行评论。

更多回答(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 个评论
Bamelari Jovani
Bamelari Jovani 2024-7-18
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
Ayush Modi 2024-7-18
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Assumptions 的更多信息

产品


版本

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by