symbolic error in calculation
5 views (last 30 days)
Show older comments
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
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
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
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 Comments
Walter Roberson
on 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
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
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)
Answers (1)
Bhavana Ravirala
on 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 Comments
Walter Roberson
on 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
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!