Error using sym>convertChar(line 1821) Character vectors and strings in the first argument can only specify a variable or number.

6 visualizzazioni (ultimi 30 giorni)
So I have this simple code:
syms y u t s Y F
F = laplace('4*diff(y(t),t) + y(t) - 3*2',s)
And it gives me this error:
Error using sym>convertChar (line 1821)
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 1490)
S = convertChar(x);
^^^^^^^^^^^^^^
Error in sym (line 259)
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{:});
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
What should I change in this so that I get the required output?

Risposta accettata

Umar
Umar il 12 Ott 2024

Hi @David Ayomide ,

After reviewing your comments and going through documentation listed at link below.

https://www.mathworks.com/help/symbolic/sym.laplace.html

Here is how you can resolve this problem, you need to ensure that the input provided to the laplace function is in the correct format. The error message indicates that a character vector or string was used instead of a symbolic expression. Here are the required changes listed below.

Define Variables Properly: Use syms to define all variables before using them in expressions.

Correctly Formulate the Expression: Ensure that the expression for which you want to compute the Laplace transform is correctly specified as a symbolic expression.

Here is how you can modify your code to eliminate the error and successfully compute the Laplace transform:

% Define symbolic variables
syms y(t) t s
% Define the expression for which we want to find the Laplace transform
expr = 4*diff(y(t), t) + y(t) - 3*2;
% Compute the Laplace transform
F = laplace(expr, t, s);
% Display the result
disp(F);

Please see attached.

syms y(t) t s: This line defines `y` as a function of `t`, along with `t` and `s` as symbolic variables.

Expression Definition: The expression `4*diff(y(t), t) + y(t) - 3*2` is now properly formatted as a symbolic expression.

Laplace Transform Calculation: The function call `laplace(expr, t, s)` specifies both the independent variable (`t`) and the transformation variable (`s`).

When you will run this updated code snippet in MATLAB, it should execute without errors and display the result of the Laplace transform of your defined expression.

Here are some considerations that I would like to share.

Understanding Laplace Transforms: The Laplace transform is particularly useful in engineering fields, especially in control theory and signal processing, as it simplifies differential equations into algebraic equations.

Error Debugging: Familiarizing yourself with MATLAB's symbolic toolbox can help prevent similar issues. Always check that your variables are defined correctly and that you are using appropriate types for operations.

This approach will ensure accuracy in computation while also providing clarity on how to work with symbolic expressions in MATLAB effectively.

Hope this helps.

  3 Commenti
Walter Roberson
Walter Roberson il 12 Ott 2024
It used to be legal to pass a character vector to ilaplace, but that was discontinued a number of releases ago.
Steven Lord
Steven Lord il 12 Ott 2024
IIRC people used to get annoyed when this (using str2sym to get what I believe was the old behavior) didn't work the way they thought it would.
syms y
a = 4;
sol = solve(str2sym('y^2==a'))
No, Symbolic Math Toolbox didn't go looking in the workspace from which you tried to convert the text into a symbolic expression to see if any of the variables in there happened to match something in the text. Nor does it do that now. But if you specify the input as a symbolic expression not as text:
sol = solve(y^2==a)

Accedi per commentare.

Più risposte (0)

Prodotti


Release

R2024b

Community Treasure Hunt

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

Start Hunting!

Translated by