Error using sym>convertChar (line 1537) when solving an ODE using laplace transform. How to fix the problem?

%%CODE
%% Solving 2nd order ODE using laplace transform
clc, clear , close all
syms x t s X F
F = laplace('diff(x(t),t,t)+7*diff(x(t),t)+10*x(t)= 20',s); % solving using laplace transform
F = subs(F,{'x(0)','D(x)(0)'},{5,3}); % initial values
F = subs(F,{'laplace(x(t),t,s)'},{X}); % substituting the initial values then solve Laplace
X = solve(F,'X');
X = ilaplace(X);
X = simplify(X); pretty(X);
disp(X)
%% ERROR BELOW
Error using sym>convertChar (line 1537)
Character vectors and strings in the first argument can only
specify a variable or number. To evaluate character vectors and
strings representing symbolic expressions, use 'str2sym'.
Error in sym>tomupad (line 1253)
S = convertChar(x);
Error in sym (line 220)
S.s = tomupad(x);
Error in transform (line 22)
if ~isa(f, 'sym'), f = sym(f); end
Error in sym/laplace (line 28)
L = transform('symobj::laplace', 't', 's', 'z', F, varargin{:});
Error in HW_1_3_2_4070H300 (line 4)
F = laplace('diff(x(t),t,t)+7*diff(x(t),t)+10*x(t)= 20',s); %
solving using laplace transform

2 Comments

Since R2017b (I think it is) you cannot pass character vectors to laplace() . You need to construct the symbolic equation and pass that instead.

Sign in to comment.

 Accepted Answer

Eliminate the single quotes, use double equal signs in the symbolic expression, express ‘x’ as ‘x(t)’ in the syms declaration (otherwise, ‘x’ is assumed to be a constant), and it works —
syms x(t) t s X F
Dx = diff(x);
D2x = diff(Dx);
F = laplace(D2x+7*Dx+10*x(t) == 20,s); % solving using laplace transform
F = subs(F,{x(0),Dx(0)},{5,3}); % initial values
F = subs(F,{laplace(x(t),t,s)},{X}); % substituting the initial values then solve Laplace
X = solve(F,X);
X = ilaplace(X);
X = simplify(X); pretty(X);
exp(-2 t) 6 - exp(-5 t) 3 + 2
disp(X)
Character arrays have not been allowed for the last few releases. (The one exception that remains is in the sym funciton.)

4 Comments

Firstly thank you
I have notived t is not needed ..Why so?
%% Solving 2nd order ODE using laplace transform
clc, clear , close all
% syms x(t) t s X F
syms x(t) s X F
Dx = diff(x);
D2x = diff(Dx);
F = laplace(D2x+7*Dx+10*x(t) == 20,s); % solving using laplace transform
F = subs(F,{x(0),Dx(0)},{5,3}); % initial values
F = subs(F,{laplace(x(t),s)},{X}); % substituting the initial values then solve Laplace
%F = subs(F,{laplace(x(t),t,s)},{X}); % t is not really needed
X = solve(F,X);
disp('laplace solution = '),disp(X)
X = ilaplace(X);
X = simplify(X); %pretty(X);
disp('inverse laplace Solution = '),disp(X)
As always, my pleasure!
Declaring ‘t’ specifically is not absolutely required (in this instance), since it is implied in declaring ‘x(t)’. It is needed in that declaration because ‘x(t)’ needs to be defined as a function in the syms declaration in order for the code using it to work (specifically with respect to defining and calculating the detrivatives of ‘x(t)’).

Sign in to comment.

More Answers (1)

%%CODE
%% Solving 2nd order ODE using laplace transform
syms x(t) s X F
Dx = diff(x,t);
D2x = diff(Dx,t);
eqn = D2x + 7*Dx + 10*x(t) == 20
eqn(t) = 
F = laplace(eqn,s) % solving using laplace transform
F = 
F = subs(F,{x(0), Dx(0)},{5,3}) % initial values
F = 
F = subs(F, {laplace(x(t),t,s)},{X}) % substituting the initial values then solve Laplace
F = 
X = solve(F, X)
X = 
X = ilaplace(X)
X = 

2 Comments

Firstly thank you
I have notived t is not needed ..Why so?
%% Solving 2nd order ODE using laplace transform
clc, clear , close all
% syms x(t) t s X F
syms x(t) s X F
Dx = diff(x);
D2x = diff(Dx);
F = laplace(D2x+7*Dx+10*x(t) == 20,s); % solving using laplace transform
F = subs(F,{x(0),Dx(0)},{5,3}); % initial values
F = subs(F,{laplace(x(t),s)},{X}); % substituting the initial values then solve Laplace
%F = subs(F,{laplace(x(t),t,s)},{X}); % t is not really needed
X = solve(F,X);
disp('laplace solution = '),disp(X)
X = ilaplace(X);
X = simplify(X); %pretty(X);
disp('inverse laplace Solution = '),disp(X)

Sign in to comment.

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!