while iam trying to execute my code iam getting error

3 Ansichten (letzte 30 Tage)
prithvi
prithvi am 23 Jan. 2024
Kommentiert: prithvi am 23 Jan. 2024
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.

Akzeptierte Antwort

Rishi
Rishi am 23 Jan. 2024
Hi prithvi,
I understan that you want to know why you are getting the given error.
The error you're encountering is due to trying to perform an operation that mixes symbolic and numeric computations in MATLAB. You are using 'sym' to define 'x' as symbolic variable and then defining 'y' as a symbolic expression in terms of 'x'. The variable 'h' is being used as a numeric array, but you're trying to assign a symbolic expression, 'y', to 'h(2)'.
To fix this error, you can define 'h' as a symbolic array from the start. Here is the edited code:
syms x; % Define x as a symbolic variable
y = 2*x - 1; % Define y as a symbolic expression
n = input('Enter the number of polynomials required: '); % Get the number of polynomials from the user
h = sym('h', [1 n]); % Initialize h as a symbolic array of size n
h(1) = 1; % Set the first Hermite polynomial
h(2) = 2*y; % Set the second Hermite polynomial
for i = 3:n
h(i) = 2*y*h(i-1) - 2*(i-2)*h(i-2); % Recurrence relation for Hermite polynomials
end
disp(h); % Display the Hermite polynomials
k = sym('k', [1 n]); % Initialize k as a symbolic array of size n
for i = 1:n
k(i) = (2/sqrt(pi))*h(i); % Scale the Hermite polynomials
end
disp(k); % Display the scaled Hermite polynomials
This will result in 'h' and 'k' both being a symbolic array. If you wish to evaluate these polynomials at specific numeric values, you can use the 'subs' function to substitute a numeric value for 'x'.
You can learn more about the 'subs' function here:
Hope this helps!

Weitere Antworten (1)

Dyuman Joshi
Dyuman Joshi am 23 Jan. 2024
Bearbeitet: Dyuman Joshi am 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;
y = 
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 = 
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)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by