symbolic error in calculation

조회 수: 5 (최근 30일)
yogeshwari patel
yogeshwari patel 2022년 11월 13일
댓글: yogeshwari patel 2022년 11월 23일
syms x
syms a
syms t
syms f % f fractional order
syms r % r cut
% syms m
% m=0.7;
U=zeros(1,2,'sym');
A=zeros(1,2,'sym');
B=zeros(1,2,'sym');
series(x,t)=sym(zeros(1,1));
U(1)=(r-1)/((1+exp(x))^2);
r=0.3
r = 0.3000
for k=1:4
A(1)=0;
for i=1:k
A(1)=A(1)+U(i)*U(k-i+1) ;
end
U(k+1)=simplify(gamma((f*(k-1)+1))/gamma((f*(k)+1))*(diff(U(k),x,2)+6*U(k)-6*A(1)));
end
disp (U)
for k=1:5
series(x,t)=series(x,t)+U(k)*(power(t,k-1));
end
series
series(x, t) = 
C=zeros(1,1);
for x=1:5
e=x;
for t=1:5
f=(t)/10;
C(x,t)=series(e,f);
end
end
Unable to perform assignment because value of type 'sym' is not convertible to 'double'.

Caused by:
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for variables.
vpa(C,15)
The last for loop is not evaluating the numerical value .Previously when I use the code the values were evaluated but now its showing the error.
  댓글 수: 5
Walter Roberson
Walter Roberson 2022년 11월 17일
syms x
syms a
syms t
syms f % f fractional order
syms r % r cut
% syms m
% m=0.7;
U=zeros(1,2,'sym');
A=zeros(1,2,'sym');
B=zeros(1,2,'sym');
series(x,t)=sym(zeros(1,1));
r=0.3
r = 0.3000
U(1)=(r-1)/((1+exp(x))^2);
for k=1:4
A(1)=0;
for i=1:k
A(1)=A(1)+U(i)*U(k-i+1) ;
end
U(k+1)=simplify(gamma((f*(k-1)+1))/gamma((f*(k)+1))*(diff(U(k),x,2)+6*U(k)-6*A(1)));
end
disp (U)
You defined U in terms of the symbolic variable f
for k=1:5
series(x,t)=series(x,t)+U(k)*(power(t,k-1));
end
series
series(x, t) = 
and you can see the symbolic variable f as part of the value of the function series, but f is not named as one of the parameters of series
C=zeros(1,1,'sym');
for x=1:5
e=x;
for t=1:5
f=(t)/10;
There you redefine f at the MATLAB level as numeric, but as I described earlier that has no effect on the symbolic f that has already been used
C(x,t)=series(e,f);
That absolutely does not cause symbolic f inside series to be replaced by the numeric value of f . It causes symbolic t inside series to be substituted with numeric f . Your f and t are distinct symbolic variables, and parameter passing is done strictly positionally.
end
end
vpa(C,15)
ans = 
yogeshwari patel
yogeshwari patel 2022년 11월 23일
thank you

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

답변 (1개)

Bhavana Ravirala
Bhavana Ravirala 2022년 11월 16일
The last for loop is failing because you are trying to assign sym variable (output of ‘series’ is a sym variable) to double variable. To eliminate this error, define C as a sym variable which gives a numeric output.
C=zeros(1,1,sym);
  댓글 수: 2
Walter Roberson
Walter Roberson 2022년 11월 17일
No, that is not the cause of the problem, or at least not directly. Consider for example,
syms x
C = zeros(1, 5);
for K = 1 : 5
C(K) = int(sin(x)/(x+1).^2, x, 0, K);
end
C
C = 1×5
0.1803 0.3387 0.3896 0.3745 0.3435
In this case the symbolic value returned by int() was converted to double for storage into double precision C.
The problem is directly not that the destination is double precision: the problem was that the result of the evaluation turned out to have unbound symbolic variables so the expression could not be numerically approximated.
The same problem can happen if you have expressions that do not always converge acceptably, such as an integration that is too steep for numeric evaluation within the default number of decimal places: there might be some values of parameters that you can get convergence for (and so approximate well enough to convert to double precision) whereas other values of the parameter might not converge well enough. The problem is not exactly that the output location is double: the problem occurs only if a numeric approximation cannot be done such as because of convergence or unbound variables.
yogeshwari patel
yogeshwari patel 2022년 11월 23일
thank you

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

카테고리

Help CenterFile Exchange에서 Conversion Between Symbolic and Numeric에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by