Error using sym>convertChar (line 1459)

13 次查看(过去 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.
1.PNG
original code from this
2.PNG

回答(3 个)

Uriel Gabriel Zapata Rodríguez
编辑: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
%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