symbolic error in calculation
Ältere Kommentare anzeigen
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 Kommentare
Walter Roberson
am 13 Nov. 2022
Bearbeitet: Walter Roberson
am 13 Nov. 2022
series(x,t)=sym(zeros(1,1));
That is a symbolic function
series(x,t)=series(x,t)+U(k)*(power(t,k-1));
That is redefining the symbolic function each time. Are you sure that is a good idea?
If I were building up a symbolic function stepwise I would probably instead build up a symbolic expression and convert it to function later. Since you are producing a double output then I would probably use matlabFunction to do the conversion.
yogeshwari patel
am 13 Nov. 2022
Bearbeitet: Walter Roberson
am 17 Nov. 2022
Walter Roberson
am 17 Nov. 2022
Bearbeitet: Walter Roberson
am 17 Nov. 2022
syms x
syms a
syms t
syms f % f fractional order
syms r % r cut
You define r as a symbolic variable. That sends a request to the separate symbolic engine process to define a variable to be called r at the level of the symbolic engine, and for the symbolic engine to send back a reference to the resulting symbolic expression. So at the MATLAB level what will get stored is something like
r = struct('digits', 32, 's', '__ans[315,15315]', 'mathcad', '');
where __ans[315,15315] is an internal symbolic engine reference to the symbolic expression. The MATLAB level does not have any idea what the symbolic expresison means -- every time it needs to work with the symbolic expression, it will ask the symbolic engine to do the work on its behalf.
% 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);
You define U(1) in terms of the symbolic variable r . When the r is encountered on the right hand side, MATLAB would look up in the symbol table to find the struct('digits', 32, 's', struct('digits', 32, 's', '__ans[315,15315]', 'mathcad', ''), 'mathcad', '') or whatever, and to subtract 1 would end up executing something like
feval(symengine, '_plus', '__ans[315,15315]', -1)
which would involve the symbolic engine looking in its internal tables to figure out what __ans[315,15315] refers to, and subtract one from it, and store the value internally, and to return a reference to the new result. So the result of the r-1 at the MATLAB level might be something like struct('digits', 32, 's', '__ans[2154,8093]', 'mathcad', '') . Again, MATLAB has no idea what the response means, just that it got told the result was a symbolic expression that can be referred to by __ans[2154,8093] . Likewise with x being a reference to an expression in the symbolic engine, exp(x) gives back a reference to a result, and 1+ that gives back a reference to a result, and ^2 that gives back a reerence to a result, and eventually MATLAB asks feval(symengine, '_divide', '__ans[2154,8093]', '__ans[44835,42]') and gets back another reference to a symbolic expression, and the wrapper object is what gets stored in U(1)
r=0.3
Notice that is a numeric 0.3, not a symbolic 0.3. So MATLAB discards the r = struct('digits', 32, 's', '__ans[315,15315]', 'mathcad', ''); that it had previously saved, and stores a plain numeric 0.3 against r, discarding the existing reference. This does not affect anything in the symbolic engine. Any reference to r inside the symbolic engine continues to refer to the symbolic r that was created at the time of the syms r
The situration is very similar to the case where you had MATLAB code like
r = 1
s = r * 2
r = 5
s
After you used r and then changed r, does s change value from being 1*2 to being 5*2 ? No! The value of r at the time it is used is what is incorporated into s. And when you defined syms r and then used r inside U(1,1) you got the symbolic r in the expression in U(1,1) and the value of that expression does not change when you assign numeric 0.3 to r after you create U(1,1)
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)
yogeshwari patel
am 23 Nov. 2022
Antworten (1)
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
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.
yogeshwari patel
am 23 Nov. 2022
Kategorien
Mehr zu Assumptions finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!








