Error using sym>convertChar (line 1459)
조회 수: 20 (최근 30일)
이전 댓글 표시
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.

original code from this

댓글 수: 0
답변 (3개)
Uriel Gabriel Zapata Rodríguez
2020년 4월 24일
편집: Steven Lord
2020년 4월 24일
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
Steven Lord
2020년 4월 24일
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.
Uriel Gabriel Zapata Rodríguez
2020년 4월 24일
%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)
댓글 수: 0
Steven Lord
2020년 4월 24일
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)."
댓글 수: 0
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!