Azzera filtri
Azzera filtri

Error using input comand

3 visualizzazioni (ultimi 30 giorni)
sandy Fleming
sandy Fleming il 16 Mar 2016
Commentato: Stalin Samuel il 17 Mar 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 Commento
Vaclav Rimal
Vaclav Rimal il 16 Mar 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.

Accedi per commentare.

Risposte (1)

Stalin Samuel
Stalin Samuel il 16 Mar 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 Commenti
sandy Fleming
sandy Fleming il 17 Mar 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 il 17 Mar 2016
X = [0.5 -3 3 -4] .no need to use the command input

Accedi per commentare.

Community Treasure Hunt

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

Start Hunting!

Translated by