problems with symbolic calculations
1 次查看(过去 30 天)
显示 更早的评论
Hi, I'm getting an error while trying to do a calculation with a symbolic expression.
First I'll write the code so I can explain my question.
Earlier in the code I defined what the variable n is, n is a cell of size 2x1 where:
n{1}
is a symbolic expression with symbolic variable x and
n{2}
is the second symbolic expression with variable x.
Also the variable L has been defined before in the code, to simplify let's suppose that
L = [2 3];
Likewise the variable c has been defined before like this:
%Physical constants definition
c = 299792458; %Speed of light in [m/s] , needs to be converted to micrometer.
c = c.*10^6; %Conversion to micrometers.
Then my problem arises when I try to do the following calculations:
d = length(n);
t1 = zeros(1,d);
for i=1:d
t1(i) = n{i}.*L(i)./c;
end
Then I get the following error from matlab:
The following error occurred converting from sym to double:
DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use VPA.
I have allready tried to use vpa like this:
for i=1:d
t1(i) = vpa(n{i}.*L(i)./c);
end
but I get the same error.
Can someone tell me how to avoid this error and be able to complete the calculations?
Thanks in advance for the effort and time.
0 个评论
采纳的回答
John D'Errico
2016-9-19
编辑:John D'Errico
2016-9-19
Simple problem. Simple to fix too. Look in this loop.
t1 = zeros(1,d);
for i=1:d
t1(i) = n{i}.*L(i)./c;
end
What is t1? What type is it? (Answer: Double precision.)
What is in the right hand side of that expression inside the loop? (Answer: symbolic stuff.)
What was the error message? Cannot convert sym to double. Why not? Because it contains a symbolic variable, x of course! You already said that n{i} was a function of x.
Instead, preallocate t1 as
t1 = zeros(1,d,'sym');
Now there will be no error, because the variable types will be compatible.
And, no, vpa cannot solve it, because vpa won't turn a symbolic expression that contains a variable into a double. vpa will still leave it symbolic.
If you don't believe me, TRY IT! Here is what you did.
t1 = zeros(1,2);
syms x
t1(1) = 3*x
The following error occurred converting from sym to double:
DOUBLE cannot convert the input expression into a double array.
Of course, vpa won't help, as I said, since 3*x is still symbolic, no matter what you do.
t1(1) = vpa(3*x)
The following error occurred converting from sym to double:
DOUBLE cannot convert the input expression into a double array.
Allocate t1 properly though...
t1 = zeros(1,2,'sym');
t1(1) = 3*x
t1 =
[ 3*x, 0]
And, finally, the last step is to show that had the result been in numeric form, it would have worked. Lets make t1 a double vector again, and try this:
t1 = zeros(1,2);
t1(1) = subs(3*x,x,pi)
t1 =
9.42477796076938 0
whos t1
Name Size Bytes Class Attributes
t1 1x2 16 double
As you can see, MATLAB still thinks that t1 is a double. So there was only a problem when you tried to stuff a square peg into a round hole.
更多回答(1 个)
Trang Nguyen
2019-7-5
Thank you so much John D'Errico. It helps me a lot in my working. You are completely great. Thanks ^^
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!