there is an error which is not being corrected. Only its taking x as an input variable.

4 views (last 30 days)
Satya Harika on 30 Nov 2021
Answered: Cris LaPierre on 28 Dec 2021 at 0:37
clc
clear
str2sym x
ans =
x
str2sym a0
ans = str2sym a1
ans = str2sym a2
ans = str2sym a3
ans = y= a0 + a1*x + a2*x^2 + a3*x^3 ;
Unrecognized function or variable 'a0'.
dy = diff(y);
d2y = diff(dy);
gde = collect( d2y-2*x^2*dy+y,x ) ;
cof =coeffs( gde , x ) ;
A2=solve(cof(1),a2);
A3=solve(cof(2),a3);
y= subs ( y , { a2 , a3 } ,{A2 , A3 } ) ;
y= coeffs( y , [ a1 a0 ] ) ;
disp( 'Solution is ' )
disp ( [ ' y=A ( ' , char ( y ( 1 ) ) , ' + . . . ) + B ( ' , char ( y ( 2 ) ) , ' + . . . ) ' ] )
ERROR:
Unrecognized function or variable 'a0'.
Error in series (line 8)
y= a0 + a1*x + a2*x^2 + a3*x^3 ;

Cris LaPierre on 28 Dec 2021 at 0:37
I don't think you want to use str2sym to declare your symbolic variables. It is used to evaluate string representing symbolic expression. I think you should just use syms.
syms x a0 a1 a2 a3
y= a0 + a1*x + a2*x^2 + a3*x^3 ;
dy = diff(y);
d2y = diff(dy);
gde = collect( d2y-2*x^2*dy+y,x ) ;
cof =coeffs( gde , x ) ;
A2=solve(cof(1),a2);
A3=solve(cof(2),a3);
y= subs ( y , { a2 , a3 } ,{A2 , A3 } ) ;
y= coeffs( y , [ a1 a0 ] ) ;
disp( 'Solution is ' )
Solution is
disp ( [ ' y=A ( ' , char(y(1)), ' + . . . ) + B ( ' , char(y(2)), ' + . . . ) ' ] )
y=A ( 1 - x^2/2 + . . . ) + B ( x - x^3/6 + . . . )