the root of the equation, OSADA, why isnt working my script?

1 view (last 30 days)
function[x1,eps]=Osada()
f=input('give function =');
xo=input('give initial value=');
tol=input('give tolerance=');
m=input('give the multiplicity of the root =');
nmax=input('give maximum no steps=');
dev1=diff(f,x,1);
dev2=diff(f,x,2);
f=inline(f);
dev1=inline(dev1);
dev2=inline(dev2);
eps=tol+1;
niter=0;
while eps>=tol&&niter<=nmax
x1=xo-1/2*m*(m+1)*f(xo)/dev1(xo)+1/2*(m-1)^2*dev1(xo)/dev2(xo);
eps=abs(x1-xo)/x1;
xo=x1;
niter=niter+1;
end
end
  4 Comments
N/A
N/A on 3 Feb 2015
thats what I get
>> Osada
give function ='exp(-x)+sin(x)'
give initial value=2
give tolerance=2
give the multiplicity of the root =2
give maximum no steps=2
??? Undefined function or variable 'x'.
Error in ==> Osada at 13
dev1=diff(f,x,1);

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 3 Feb 2015
Edited: John D'Errico on 3 Feb 2015
Sorry, I don't want to chastise you, but you need to think about what you are doing when you see an error. What does the error tell you? Here, you waited 10 hours until you actually provided enough information about what you were doing for me to know what the immediate problems was. And then you had to wait until I looked back in 4 hours later to get a response.
And given that the error message seems clear, the point is, READ the error message.
Off my soapbox now. So what did it tell you?
??? Undefined function or variable 'x'.
Error in ==> Osada at 13
dev1=diff(f,x,1);
Ok, so MATLAB does not know what x is. x is "UNDEFINED". So where in your function have you indicated that x was a variable? Where did you tell MATLAB that x was a symbolic variable?
In MATLAB, a fundamental concept about functions (That sadly many people do not seem to learn, so you are not even remotely alone here. I think that either somehow a failure in the tutorials, or a lack of reading those same tutorials.) is that functions have their own workspaces. A variable does not exist inside a function unless it is passed in, or it is created, or unless it is a nested function, so it can also see the workspace of the caller function.
None of those cases apply here. So when MATLAB tries to execute diff here, it looks at x, and sees no place where x was defined. That gets MATLAB upset, so it generates the rather clear error message you saw. Yes, it may seem to you that MATLAB might be able to figure it out. But computers are very literal things. You tried to pass something called x into the function diff. So MATLAB looks around, and it sees nothing called x.
Yes, from farther out, we can see that x is a variable inside the expression 'exp(-x)+sin(x)', which you also have passed into diff. But that is simply a character string. No more, no less. Computers don't understand what you want them to do. They do what you tell them to do.
The point is, you had all the information necessary in your hands. Again, I'm trying to show you how to read an error message. Think about what MATLAB is telling you. It will save you time, rather than waiting for someone to read the error message to then tell you the same thing, though perhaps in more words.
  2 Comments
N/A
N/A on 3 Feb 2015
not too much help, im stuck again, dont know what to do
John D'Errico
John D'Errico on 3 Feb 2015
Edited: John D'Errico on 3 Feb 2015
So what are you stuck on? I mean, be serious, how can I guess what is your problem now? Maybe you need to learn to use the debugger. Or not. The crystal ball is so cloudy.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!