while iam trying to execute my code iam getting error
이전 댓글 표시
x = sym('x');
y=2*x-1
h(1)=1;
h(2)=2*y;
n=input('enter number of polynomials required ');
for i=3:n
h(i)=2*y*h(i-1)-2*(i-2)*h(i-2);
end
disp(h);
for i=1:n
k(i)=(2/sqrt(pi))*h(i);
end
disp(k)
error is
Error in hermite (line 4)
h(2)=2*y;
Caused by:
Error using symengine
Unable to convert expression containing symbolic variables into double array. Apply 'subs' function first to substitute values for
variables.
채택된 답변
추가 답변 (1개)
Numeric arrays in MATLAB are homogenous.
When you initialize h(1) as a double array, then any elements to be added to it are expected to be double or something that can be converted to a double (of course, of compatible size for concatenation).
But since 2*y is a symbolic variable, it can't be converted to a double array, thus you get the error, which states the same as well.
What can you do now?
You can preallocate the array h as a symbolic array -
x = sym('x');
y = 2*x-1;
n = 5; %input('enter number of polynomials required ');
%One of the option is to preallocate the arrays as with zeros()
h = zeros(1, n, 'sym')
h(1) = 1;
h(2) = 2*y;
for i=3:n
h(i) = 2*y*h(i-1)-2*(i-2)*h(i-2);
end
disp(h);
Also, you can write k as follows -
%Vectorized form
k = (2/sqrt(sym('pi')))*h;
disp(k)
카테고리
도움말 센터 및 File Exchange에서 Polynomials에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
