Error using input comand
3 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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
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.
Risposte (1)
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
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!