there is an error which is not being corrected. Only its taking x as an input variable.
Show older comments
clc
clear
str2sym x
str2sym a0
str2sym a1
str2sym a2
str2sym 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 ' )
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 ;
Answers (1)
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 ' )
disp ( [ ' y=A ( ' , char(y(1)), ' + . . . ) + B ( ' , char(y(2)), ' + . . . ) ' ] )
Categories
Find more on Operations on Strings 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!