Borrar filtros
Borrar filtros

Error using input comand

3 visualizaciones (últimos 30 días)
sandy Fleming
sandy Fleming el 16 de Mzo. de 2016
Comentada: Stalin Samuel el 17 de Mzo. de 2016
Hey
Im struggeling to use the input command when solving an equation using the Newton Raphson method. my whole code is as follow;
clear all
close all
clc
a=input((0.5)*x^3 -3*x^2 +3*x -4,'x');
x(1)=input(4);
error=input(1);
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)
when I enter;
  • a=input((0.5)*x^3 -3*x^2 +3*x -4,'x');
I recieve the following error;
  • Undefined function or variable 'x'.
If i define X before hand i recieve this error;
  • Error using inputThe first argument to INPUT must be a string.
if i use;
  • a=@(x) input((0.5)*x^3 -3*x^2 +3*x -4,'x');it brings up the same error but for the x(1) line
it would be very helpful to know why this happens and how to fix the code
thanks
sandy
  1 comentario
Vaclav Rimal
Vaclav Rimal el 16 de Mzo. de 2016
What do you expect your first input command would return? The way you write your code does not make any sense; input takes just the string as an arguments.

Iniciar sesión para comentar.

Respuestas (1)

Stalin Samuel
Stalin Samuel el 16 de Mzo. de 2016
  • Are you expecting like this
clear all
close all
clc
a='(0.5)*x^3 -3*x^2 +3*x -4';
x=input('Enter th x vector');%e.g[2 4 5 56 6 3]
error=input('Enter the error limit');%e.g 2
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)
  3 comentarios
sandy Fleming
sandy Fleming el 17 de Mzo. de 2016
Ive tried exactly what you said but i still get an error at the line
X=input(0.5 -3 3 -4)
the 3 is underlined in red with the error; unexpected matlab expression appearing in the command window.
Stalin Samuel
Stalin Samuel el 17 de Mzo. de 2016
X = [0.5 -3 3 -4] .no need to use the command input

Iniciar sesión para comentar.

Community Treasure Hunt

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

Start Hunting!

Translated by