Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
3rd derivative function

Subject: 3rd derivative function

From: Nina

Date: 10 May, 2012 15:28:20

Message: 1 of 10

Hello everybody,

I know this is really simple but I can't see what's wrong now. I'm trying to write a 3rd derivative approximation that uses the first derivative, of an initial function that is the exponential. I've been trying to run this code:

if x >=0
    then f(x) = exp((x));
end
if x <0
    then f(x) = 1/(exp((-x)));
end
f1(x) = (f(x+h)-f(x-h))/(2*h);
f3(x) = (3/(h^3))*(f(x+h)-f(x-h)-2*h*f1(x))

but MATLAB refuses it saying

Undefined function 'f' for input arguments of type 'double'.

Please, does anyone have any idea what could be wrong?
Thanks in advance!

Subject: 3rd derivative function

From: Nasser M. Abbasi

Date: 10 May, 2012 15:42:43

Message: 2 of 10

On 5/10/2012 10:28 AM, Nina wrote:

>I've been trying to run this code:
>
> if x>=0
> then f(x) = exp((x));
> end
> if x<0
> then f(x) = 1/(exp((-x)));
> end
> f1(x) = (f(x+h)-f(x-h))/(2*h);
> f3(x) = (3/(h^3))*(f(x+h)-f(x-h)-2*h*f1(x))
>
> but MATLAB refuses it saying
>
> Undefined function 'f' for input arguments of type 'double'.
>

Is the above the complete code? nothing before it? that is
all?

Ok, lets think about it.

So Matlab first sees this

> if x>=0
> then f(x) = exp((x));
> end

Then it looks around for 'x' to see if it >= or not.

But it does not see 'x' anywhere. What is it supposed to
do now? How is it going to decide is x is less than 0 or
not? It can't go to the next line. It is not a compiler, it
has to execute this line now before the next.

Lets resolve this issue first before we look at the
rest of the code?

--Nasser

Subject: 3rd derivative function

From: Nina

Date: 10 May, 2012 15:56:13

Message: 3 of 10

Thank you, Nasser!

The full code is this:

h = input ('Type the value of h: ');
x = input ('Type the value of x: ');
h = str2num('h');
x = str2num('x');
if x >=0
    then f(x) = exp((x));
end
if x <0
    then f(x) = 1/(exp((-x)));
end

f1(x) = (f(x+h)-f(x-h))/(2*h);
f3(x) = (3/(h^3))*(f(x+h)-f(x-h)-2*h*f1(x))

I was thinking it had the necessary values for evaluating the function, but apparently, it still doesn't...

Subject: 3rd derivative function

From: Nina

Date: 10 May, 2012 15:57:07

Message: 4 of 10

Thank you, Nasser!

The full code is this:

h = input ('Type the value of h: ');
x = input ('Type the value of x: ');
h = str2num('h');
x = str2num('x');
if x >=0
    then f(x) = exp((x));
end
if x <0
    then f(x) = 1/(exp((-x)));
end

f1(x) = (f(x+h)-f(x-h))/(2*h);
f3(x) = (3/(h^3))*(f(x+h)-f(x-h)-2*h*f1(x))

I was thinking it had the necessary values for evaluating the function, but apparently, it still doesn't...

Subject: 3rd derivative function

From: Nasser M. Abbasi

Date: 10 May, 2012 16:06:24

Message: 5 of 10

On 5/10/2012 10:57 AM, Nina wrote:
> Thank you, Nasser!
>
> The full code is this:
>
> h = input ('Type the value of h: ');
> x = input ('Type the value of x: ');
> h = str2num('h');
> x = str2num('x');
> if x>=0
> then f(x) = exp((x));
> end
> if x<0
> then f(x) = 1/(exp((-x)));
> end
>
> f1(x) = (f(x+h)-f(x-h))/(2*h);
> f3(x) = (3/(h^3))*(f(x+h)-f(x-h)-2*h*f1(x))
>
> I was thinking it had the necessary values for evaluating the function, but apparently, it still doesn't...


my version of Matlab does not have 'then'. I do not know what version
you have that has this keyword.

But try this

---------------------------
h=0.1;
x=1;

if x >=0
     f = @(x) exp((x));
else
     f = @(x) 1/(exp((-x)));
end

f1 = (f(x+h)-f(x-h))/(2*h)
f3 = (3/(h^3))*(f(x+h)-f(x-h)-2*h*f1(x))
---------------------------------------------

No need to deal with input from keyboard at initial
pass. Just define your variables in the code. Once
the algorithm works ok, then you can go fancy
and read the input from the keyboard. Do one thing at
a time. Also I think the way you are reading the input
from keyboard is not quite right.

--Nasser

Subject: 3rd derivative function

From: Nina

Date: 10 May, 2012 16:22:26

Message: 6 of 10

I tried it, but I'm still getting the same error message... did it work at your version of MATLAB? Which version are you using?

Subject: 3rd derivative function

From: Nasser M. Abbasi

Date: 10 May, 2012 16:30:03

Message: 7 of 10

On 5/10/2012 11:22 AM, Nina wrote:

> I tried it, but I'm still getting the same error message...
>did it work at your version of MATLAB? Which version are you using?

Yes, it worked on my Matlab. I am using 2012a. Try to do clear all before
you start.

------------------------------
EDU>>

h=0.1;
x=1;


if x >=0
     f = @(x) exp((x));
else
     f= @(x) 1/(exp((-x)));
end

f1 = (f(x+h)-f(x-h))/(2*h)
f3 = (3/(h^3))*(f(x+h)-f(x-h)-2*h*f1(x))
--------------------------------------

f1 =
     2.7228

f3 =
      0

no errors. I did not look at the equartions you
had. I just run them. no error messages.

--Nasser

Subject: 3rd derivative function

From: Nina

Date: 10 May, 2012 16:44:08

Message: 8 of 10

Yess!! It's working now! Thank you so much, Nasser!

Nina

Subject: 3rd derivative function

From: Roger Stafford

Date: 10 May, 2012 16:58:25

Message: 9 of 10

"Nina " <ninakuklisova@uchicago.edu> wrote in message <jogmqk$hqn$1@newscl01ah.mathworks.com>...
> I know this is really simple but I can't see what's wrong now. I'm trying to write a 3rd derivative approximation that uses the first derivative, of an initial function that is the exponential. I've been trying to run this code:
>
> if x >=0
> then f(x) = exp((x));
> end
> if x <0
> then f(x) = 1/(exp((-x)));
> end
> f1(x) = (f(x+h)-f(x-h))/(2*h);
> f3(x) = (3/(h^3))*(f(x+h)-f(x-h)-2*h*f1(x))
- - - - - - - - -
  Besides the difficulty you are having defining f, your difference equation approximating the third derivative is incorrect. It should be:

 f'''(x) = (f(x+2*h)-f(x-2*h)-2*(f(x+h)-f(x-h)))/(2*h^3)

  This means that after receiving the value of x you must evaluate f at four different points, f(x-2*h), f(x-h), f(x+h), and f(x+2*h), and the value of f(x) is not needed. Also somewhere you need to define h.

  I am puzzled as to why you wish to distinguish between negative and non-negative values of x. The single definition

 f = @(x) exp(x)

will cover both cases since exp(x) = 1/(exp(-x) for all x.

Roger Stafford

Subject: 3rd derivative function

From: John D'Errico

Date: 10 May, 2012 17:04:30

Message: 10 of 10

"Roger Stafford" wrote in message <jogs3h$cat$1@newscl01ah.mathworks.com>...
> "Nina " <ninakuklisova@uchicago.edu> wrote in message <jogmqk$hqn$1@newscl01ah.mathworks.com>...
> > I know this is really simple but I can't see what's wrong now. I'm trying to write a 3rd derivative approximation that uses the first derivative, of an initial function that is the exponential. I've been trying to run this code:
> >
> > if x >=0
> > then f(x) = exp((x));
> > end
> > if x <0
> > then f(x) = 1/(exp((-x)));
> > end
> > f1(x) = (f(x+h)-f(x-h))/(2*h);
> > f3(x) = (3/(h^3))*(f(x+h)-f(x-h)-2*h*f1(x))
> - - - - - - - - -
> Besides the difficulty you are having defining f, your difference equation approximating the third derivative is incorrect. It should be:
>
> f'''(x) = (f(x+2*h)-f(x-2*h)-2*(f(x+h)-f(x-h)))/(2*h^3)
>
> This means that after receiving the value of x you must evaluate f at four different points, f(x-2*h), f(x-h), f(x+h), and f(x+2*h), and the value of f(x) is not needed. Also somewhere you need to define h.
>
> I am puzzled as to why you wish to distinguish between negative and non-negative values of x. The single definition
>
> f = @(x) exp(x)
>
> will cover both cases since exp(x) = 1/(exp(-x) for all x.
>
> Roger Stafford

There is at least one other error. For example,

if x <0
    then f(x) = 1/(exp((-x)));
end

But, we should know that

1/exp(-x) == exp(x)

so I'm not at all sure why one would bother to
try making a piecewise function out of this.

John

Tags for this Thread

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.

Contact us