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

1 ビュー (過去 30 日間)
Satya  Harika
Satya Harika 2021 年 11 月 30 日
回答済み: Cris LaPierre 2021 年 12 月 28 日 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 ;

回答 (1 件)

Cris LaPierre
Cris LaPierre 2021 年 12 月 28 日 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 + . . . )

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by