Error using sym>convertChar (line 1459)

19 visualizzazioni (ultimi 30 giorni)
Krittapong Pohdoy
Krittapong Pohdoy il 10 Feb 2019
Commentato: Steven Lord il 24 Apr 2020
I need some help of my code. What's wrong with this code ?
>> clear all; clf
>> syms y t x z
>> % input a unit-step (heaviside) response
>> y = dsolve('D2y + 5*Dy + 6*y = heaviside(t)','y(0) = 0','Dy(0) = 0','t');
Error using sym>convertChar (line 1459)
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 1225)
S = convertChar(x);
Error in sym (line 214)
S.s = tomupad(x);
Error in dsolve>mupadDsolve (line 327)
sys = [sys_sym sym(sys_str)];
Error in dsolve (line 189)
sol = mupadDsolve(args, options);
>>
I attach dsolve.m and heaviside.m with this form.
this is my code.
1.PNG
original code from this
2.PNG

Risposte (3)

Uriel Gabriel Zapata Rodríguez
Modificato: Steven Lord il 24 Apr 2020
i have the same problem with this code and nobody is here :'(
a=input('Enter the function in the form of variable x:','s');
x(1)=input('Enter Initial Guess:');
error=input('Enter allowed Error:');
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
for i=1:100
x(i+1)=x(i)-((f(x(i))/d(x(i))));
err(i)=abs((x(i+1)-x(i))/x(i));
if err(i)<error
break
end
end
root=x(i)
[SL: Formatted code as code]
  1 Commento
Steven Lord
Steven Lord il 24 Apr 2020
First, you shouldn't use error as the name of your variable. It already has a meaning in MATLAB.
Second, you shouldn't use inline. If you want to convert the text the user has entered into a function, use str2func to turn it into a function handle. If you want to convert it into a sym object use str2sym.

Accedi per commentare.


Uriel Gabriel Zapata Rodríguez
%newton-raphson method
a=input('Enter the function in the form of variable x:','s');
x(1)=input('Enter Initial Guess:');
error=input('Enter allowed Error:');
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
for i=1:100
x(i+1)=x(i)-((f(x(i))/d(x(i))));
err(i)=abs((x(i+1)-x(i))/x(i));
if err(i)<error
break
end
end
root=x(i)

Steven Lord
Steven Lord il 24 Apr 2020
As stated in the Note at the top of the documentation page for dsolve "Support for character vector or string inputs will be removed in a future release. Instead, use syms to declare variables and replace inputs such as dsolve('Dy = y') with syms y(t); dsolve(diff(y,t) == y)."

Tag

Prodotti


Release

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by