symbolic error in calculation

5 Ansichten (letzte 30 Tage)
yogeshwari patel
yogeshwari patel am 13 Nov. 2022
Kommentiert: yogeshwari patel am 23 Nov. 2022
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 Kommentare
Walter Roberson
Walter Roberson am 17 Nov. 2022
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 am 23 Nov. 2022
thank you

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Bhavana Ravirala
Bhavana Ravirala am 16 Nov. 2022
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 Kommentare
Walter Roberson
Walter Roberson am 17 Nov. 2022
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 am 23 Nov. 2022
thank you

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by