Thread Subject: function not working

Subject: function not working

From: halee Rustumani

Date: 21 Dec, 2009 15:04:04

Message: 1 of 8

Hello everyone I have a problem I have developed this program and theres a very irritating error thats making the program not work pleasssssse helppp
the program:
function [df]=dir(or,x0)
x=[0.2 0.4 0.6 0.8 1.0]
f=[8 5 4 2]
h=x(2)-x(1)
pos=find(x==x0)
pos1=find(x==x0+h)
pos2=find(x==(x0-h))
pos3=find(x==(x0-(x0-h)))
pos4=find(x==(x0+(2.*h)))
if(or==1)
    df=((f(pos1)-f(pos2))/(2.*h));
elseif(or==2)
    df=(f(pos1)-(2.*f(pos))+f(pos3))/h.^2;
elseif(or==3)
    df=(f(pos4)-(2.*f(pos1))+(2.*f((pos2))))/(2*(h.^3));
end
if you run it without the function the prob is in position 2 it says" Empty matrix: 1-by-0" and if you put in the function and call it the error is "??? Error using ==> dir
Too many input arguments."
test it using x0=0.6 and check
I really need help
thanx in advance

Subject: function not working

From: dpb

Date: 21 Dec, 2009 15:32:53

Message: 2 of 8

halee Rustumani wrote:
...
> the program:
> function [df]=dir(or,x0)
...

> if you run it without the function the prob is in position 2 it says"
> Empty matrix: 1-by-0"

Well, I'm not sure what you mean by "in position 2" but the error is
telling you the result of whichever operation(s) you made resulted in no
data being in the matrix. Find out why that is so and fix your logic.

> and if you put in the function and call it the
> error is "??? Error using ==> dir
> Too many input arguments."
...

Don't name your function dir; it's a ML builtin function. Use some
other non-reserved name.

--

Subject: function not working

From: checker i

Date: 21 Dec, 2009 15:39:04

Message: 3 of 8

"halee Rustumani" <halee89@hotmail.com> wrote in message <hgo2p4$jv0$1@fred.mathworks.com>...
> Hello everyone I have a problem I have developed this program and theres a very irritating error thats making the program not work pleasssssse helppp
> the program:
> function [df]=dir(or,x0)
> x=[0.2 0.4 0.6 0.8 1.0]
> f=[8 5 4 2]
> h=x(2)-x(1)
> pos=find(x==x0)
> pos1=find(x==x0+h)
> pos2=find(x==(x0-h))
> pos3=find(x==(x0-(x0-h)))
> pos4=find(x==(x0+(2.*h)))
> if(or==1)
> df=((f(pos1)-f(pos2))/(2.*h));
> elseif(or==2)
> df=(f(pos1)-(2.*f(pos))+f(pos3))/h.^2;
> elseif(or==3)
> df=(f(pos4)-(2.*f(pos1))+(2.*f((pos2))))/(2*(h.^3));
> end
> if you run it without the function the prob is in position 2 it says" Empty matrix: 1-by-0" and if you put in the function and call it the error is "??? Error using ==> dir
> Too many input arguments."
> test it using x0=0.6 and check
> I really need help
> thanx in advance

Can you try changing the function name from 'dir' to something else like 'test_dir' and see if you r getting the same error.

'dir' is a predefined function in matlab.

Subject: function not working

From: checker i

Date: 21 Dec, 2009 16:15:08

Message: 4 of 8

"halee Rustumani" <halee89@hotmail.com> wrote in message <hgo2p4$jv0$1@fred.mathworks.com>...
> Hello everyone I have a problem I have developed this program and theres a very irritating error thats making the program not work pleasssssse helppp
> the program:
> function [df]=dir(or,x0)
> x=[0.2 0.4 0.6 0.8 1.0]
> f=[8 5 4 2]
> h=x(2)-x(1)
> pos=find(x==x0)
> pos1=find(x==x0+h)
> pos2=find(x==(x0-h))
> pos3=find(x==(x0-(x0-h)))
> pos4=find(x==(x0+(2.*h)))
> if(or==1)
> df=((f(pos1)-f(pos2))/(2.*h));
> elseif(or==2)
> df=(f(pos1)-(2.*f(pos))+f(pos3))/h.^2;
> elseif(or==3)
> df=(f(pos4)-(2.*f(pos1))+(2.*f((pos2))))/(2*(h.^3));
> end
> if you run it without the function the prob is in position 2 it says" Empty matrix: 1-by-0" and if you put in the function and call it the error is "??? Error using ==> dir
> Too many input arguments."
> test it using x0=0.6 and check
> I really need help
> thanx in advance

Looks like the reson for the wrong value of 'pos2' is due the same old floating point math calculation in matlab.

Just tried u r code with
%Basically trying to avoid floating point no.
x=[0.2 0.4 0.6 0.8 1.0]*10;
and x0=0.6*10;

Subject: function not working

From: halee Rustumani

Date: 21 Dec, 2009 16:33:07

Message: 5 of 8

thanks alot dpb and checker its ok now but there is still the problem of " Empty matrix: 1-by-0" for position 2 please try to see the cause of the error, whts wierd is that all the other positions work .
thanks

Subject: function not working

From: dpb

Date: 21 Dec, 2009 16:58:14

Message: 6 of 8

halee Rustumani wrote:
> thanks alot dpb and checker its ok now but there is still the problem of
> " Empty matrix: 1-by-0" for position 2 please try to see the cause of
> the error, whts wierd is that all the other positions work .
> thanks

It's floating point comparions and expecting exact results to the last
bit as another poster hinted (but, no, it's not a "problem" w/ ML as
implied there, it's simply floating point arithmetic).

You'll have to add some tolerance to the comparisons for robustness.

See the ML wiki on floating point; I forget the exact FAQ# and don't
have time at present to look...

--

Subject: function not working

From: halee Rustumani

Date: 21 Dec, 2009 19:47:04

Message: 7 of 8

thanks alot for trying to help guys and if anyone has a solution I would really appretiate it .
thankss

Subject: function not working

From: dpb

Date: 21 Dec, 2009 19:54:27

Message: 8 of 8

halee Rustumani wrote:
> thanks alot for trying to help guys and if anyone has a solution I would
> really appretiate it .
> thankss

Did you read the wiki??? It shouldn't be necessary to point you
directly, but I'm now back from meetings so will...

<http://matlabwiki.mathworks.com/MATLAB_FAQ#Why_is_0.3-0.2-0.1_not_equal_to_zero_.28or_similar.29.3F>

I told you the solution -- put a tolerance on the floating point
comparisons. There's an example in the FAQ above.

While there, I _STRONGLY_ suggest reading the two references given as
links...

--

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

rssFeed for this Thread

Contact us at files@mathworks.com