while iam trying to execute my code iam getting error
Show older comments
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.
Accepted Answer
More Answers (1)
Dyuman Joshi
on 23 Jan 2024
Edited: Dyuman Joshi
on 23 Jan 2024
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)
Categories
Find more on Polynomials in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!