Error using sym>convertChar (line 1459)

조회 수: 20 (최근 30일)
Krittapong Pohdoy
Krittapong Pohdoy 2019년 2월 10일
댓글: Steven Lord 2020년 4월 24일
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

답변 (3개)

Uriel Gabriel Zapata Rodríguez
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
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
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)

Steven Lord
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)."

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by