How to change the answer into decimals

14 vues (au cours des 30 derniers jours)
Faris Sulam
Faris Sulam le 25 Jan 2021
Modifié(e) : VBBV le 25 Jan 2021
Hi all,
I wanted to ask about my result here. Usually it is in decimals but when i used poly2sym here it turned into fractions. How can I change it back to decimals? Thanks in advance
  2 commentaires
Birdman
Birdman le 25 Jan 2021
Can you share your entire code? It is hard to tell what you tried to do here.
Faris Sulam
Faris Sulam le 25 Jan 2021
So i was basically trying to convert the answer of the folloing code into a polynomial form:
function [ p ] = UDPolyReg( x,y )
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here
%y=[0 3 4.5 5.8 5.9 5.8 6.2 7.4 9.6 15.6 20.7 26.7 31.1 35.6 39.3 41.5]
n=length(x);
m=4;
for i=1:2*m
xsum(i)=sum(x.^(i));
end
a(1,1)=n
b(1,1)=sum(y)
for j=2:m+1
a(1,j)=xsum(j-1);
end
for i=2:m+1
for j=1:m+1
a(i,j)=xsum(j+i-2);
end
b(i,1)=sum(x.^(i-1).*y);
end
p=(a\b)'
for i=1:m+1
FuncVal(i)=p(m+2-i);
end
Vstrain=0:0.1:6;
stressfit=polyval(FuncVal,Vstrain);
plot(x,y,'ro',Vstrain, stressfit)
xlabel('Strain')
ylabel('Stress')
end

Connectez-vous pour commenter.

Réponse acceptée

VBBV
VBBV le 25 Jan 2021
Modifié(e) : VBBV le 25 Jan 2021
poly2sym converts numeric vector to vector of symbolic equation and by default it uses the variable x. The values in vector are treated as coefficient of symbolic polynomial in x.
  1 commentaire
VBBV
VBBV le 25 Jan 2021
Modifié(e) : VBBV le 25 Jan 2021
In your case vector has 5 values. So it results in polynomial in 4th power. MATLAB converted the numeric values to fractions and expressed in form of polynomial.
Use sym2poly to convert back to numeric vector.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Polynomials dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by