http://www.mathworks.com/matlabcentral/newsreader/view_thread/319944
MATLAB Central Newsreader  3rd derivative function
Feed for thread: 3rd derivative function
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 10 May 2012 15:28:20 +0000
3rd derivative function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319944#876380
Nina
Hello everybody,<br>
<br>
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:<br>
<br>
if x >=0 <br>
then f(x) = exp((x));<br>
end<br>
if x <0<br>
then f(x) = 1/(exp((x)));<br>
end<br>
f1(x) = (f(x+h)f(xh))/(2*h);<br>
f3(x) = (3/(h^3))*(f(x+h)f(xh)2*h*f1(x))<br>
<br>
but MATLAB refuses it saying <br>
<br>
Undefined function 'f' for input arguments of type 'double'.<br>
<br>
Please, does anyone have any idea what could be wrong?<br>
Thanks in advance!

Thu, 10 May 2012 15:42:43 +0000
Re: 3rd derivative function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319944#876382
Nasser M. Abbasi
On 5/10/2012 10:28 AM, Nina wrote:<br>
<br>
>I've been trying to run this code:<br>
><br>
> if x>=0<br>
> then f(x) = exp((x));<br>
> end<br>
> if x<0<br>
> then f(x) = 1/(exp((x)));<br>
> end<br>
> f1(x) = (f(x+h)f(xh))/(2*h);<br>
> f3(x) = (3/(h^3))*(f(x+h)f(xh)2*h*f1(x))<br>
><br>
> but MATLAB refuses it saying<br>
><br>
> Undefined function 'f' for input arguments of type 'double'.<br>
><br>
<br>
Is the above the complete code? nothing before it? that is<br>
all?<br>
<br>
Ok, lets think about it.<br>
<br>
So Matlab first sees this<br>
<br>
> if x>=0<br>
> then f(x) = exp((x));<br>
> end<br>
<br>
Then it looks around for 'x' to see if it >= or not.<br>
<br>
But it does not see 'x' anywhere. What is it supposed to<br>
do now? How is it going to decide is x is less than 0 or<br>
not? It can't go to the next line. It is not a compiler, it<br>
has to execute this line now before the next.<br>
<br>
Lets resolve this issue first before we look at the<br>
rest of the code?<br>
<br>
Nasser

Thu, 10 May 2012 15:56:13 +0000
Re: 3rd derivative function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319944#876385
Nina
Thank you, Nasser!<br>
<br>
The full code is this:<br>
<br>
h = input ('Type the value of h: ');<br>
x = input ('Type the value of x: ');<br>
h = str2num('h');<br>
x = str2num('x');<br>
if x >=0 <br>
then f(x) = exp((x));<br>
end<br>
if x <0<br>
then f(x) = 1/(exp((x)));<br>
end<br>
<br>
f1(x) = (f(x+h)f(xh))/(2*h);<br>
f3(x) = (3/(h^3))*(f(x+h)f(xh)2*h*f1(x))<br>
<br>
I was thinking it had the necessary values for evaluating the function, but apparently, it still doesn't...

Thu, 10 May 2012 15:57:07 +0000
Re: 3rd derivative function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319944#876386
Nina
Thank you, Nasser!<br>
<br>
The full code is this:<br>
<br>
h = input ('Type the value of h: ');<br>
x = input ('Type the value of x: ');<br>
h = str2num('h');<br>
x = str2num('x');<br>
if x >=0 <br>
then f(x) = exp((x));<br>
end<br>
if x <0<br>
then f(x) = 1/(exp((x)));<br>
end<br>
<br>
f1(x) = (f(x+h)f(xh))/(2*h);<br>
f3(x) = (3/(h^3))*(f(x+h)f(xh)2*h*f1(x))<br>
<br>
I was thinking it had the necessary values for evaluating the function, but apparently, it still doesn't...

Thu, 10 May 2012 16:06:24 +0000
Re: 3rd derivative function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319944#876387
Nasser M. Abbasi
On 5/10/2012 10:57 AM, Nina wrote:<br>
> Thank you, Nasser!<br>
><br>
> The full code is this:<br>
><br>
> h = input ('Type the value of h: ');<br>
> x = input ('Type the value of x: ');<br>
> h = str2num('h');<br>
> x = str2num('x');<br>
> if x>=0<br>
> then f(x) = exp((x));<br>
> end<br>
> if x<0<br>
> then f(x) = 1/(exp((x)));<br>
> end<br>
><br>
> f1(x) = (f(x+h)f(xh))/(2*h);<br>
> f3(x) = (3/(h^3))*(f(x+h)f(xh)2*h*f1(x))<br>
><br>
> I was thinking it had the necessary values for evaluating the function, but apparently, it still doesn't...<br>
<br>
<br>
my version of Matlab does not have 'then'. I do not know what version<br>
you have that has this keyword.<br>
<br>
But try this<br>
<br>
<br>
h=0.1;<br>
x=1;<br>
<br>
if x >=0<br>
f = @(x) exp((x));<br>
else<br>
f = @(x) 1/(exp((x)));<br>
end<br>
<br>
f1 = (f(x+h)f(xh))/(2*h)<br>
f3 = (3/(h^3))*(f(x+h)f(xh)2*h*f1(x))<br>
<br>
<br>
No need to deal with input from keyboard at initial<br>
pass. Just define your variables in the code. Once<br>
the algorithm works ok, then you can go fancy<br>
and read the input from the keyboard. Do one thing at<br>
a time. Also I think the way you are reading the input<br>
from keyboard is not quite right.<br>
<br>
Nasser

Thu, 10 May 2012 16:22:26 +0000
Re: 3rd derivative function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319944#876388
Nina
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?

Thu, 10 May 2012 16:30:03 +0000
Re: 3rd derivative function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319944#876389
Nasser M. Abbasi
On 5/10/2012 11:22 AM, Nina wrote:<br>
<br>
> I tried it, but I'm still getting the same error message...<br>
>did it work at your version of MATLAB? Which version are you using?<br>
<br>
Yes, it worked on my Matlab. I am using 2012a. Try to do clear all before<br>
you start.<br>
<br>
<br>
EDU>><br>
<br>
h=0.1;<br>
x=1;<br>
<br>
<br>
if x >=0<br>
f = @(x) exp((x));<br>
else<br>
f= @(x) 1/(exp((x)));<br>
end<br>
<br>
f1 = (f(x+h)f(xh))/(2*h)<br>
f3 = (3/(h^3))*(f(x+h)f(xh)2*h*f1(x))<br>
<br>
<br>
f1 =<br>
2.7228<br>
<br>
f3 =<br>
0<br>
<br>
no errors. I did not look at the equartions you<br>
had. I just run them. no error messages.<br>
<br>
Nasser

Thu, 10 May 2012 16:44:08 +0000
Re: 3rd derivative function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319944#876391
Nina
Yess!! It's working now! Thank you so much, Nasser!<br>
<br>
Nina

Thu, 10 May 2012 16:58:25 +0000
Re: 3rd derivative function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319944#876394
Roger Stafford
"Nina " <ninakuklisova@uchicago.edu> wrote in message <jogmqk$hqn$1@newscl01ah.mathworks.com>...<br>
> 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:<br>
> <br>
> if x >=0 <br>
> then f(x) = exp((x));<br>
> end<br>
> if x <0<br>
> then f(x) = 1/(exp((x)));<br>
> end<br>
> f1(x) = (f(x+h)f(xh))/(2*h);<br>
> f3(x) = (3/(h^3))*(f(x+h)f(xh)2*h*f1(x))<br>
        <br>
Besides the difficulty you are having defining f, your difference equation approximating the third derivative is incorrect. It should be:<br>
<br>
f'''(x) = (f(x+2*h)f(x2*h)2*(f(x+h)f(xh)))/(2*h^3)<br>
<br>
This means that after receiving the value of x you must evaluate f at four different points, f(x2*h), f(xh), f(x+h), and f(x+2*h), and the value of f(x) is not needed. Also somewhere you need to define h.<br>
<br>
I am puzzled as to why you wish to distinguish between negative and nonnegative values of x. The single definition<br>
<br>
f = @(x) exp(x)<br>
<br>
will cover both cases since exp(x) = 1/(exp(x) for all x.<br>
<br>
Roger Stafford

Thu, 10 May 2012 17:04:30 +0000
Re: 3rd derivative function
http://www.mathworks.com/matlabcentral/newsreader/view_thread/319944#876395
John D'Errico
"Roger Stafford" wrote in message <jogs3h$cat$1@newscl01ah.mathworks.com>...<br>
> "Nina " <ninakuklisova@uchicago.edu> wrote in message <jogmqk$hqn$1@newscl01ah.mathworks.com>...<br>
> > 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:<br>
> > <br>
> > if x >=0 <br>
> > then f(x) = exp((x));<br>
> > end<br>
> > if x <0<br>
> > then f(x) = 1/(exp((x)));<br>
> > end<br>
> > f1(x) = (f(x+h)f(xh))/(2*h);<br>
> > f3(x) = (3/(h^3))*(f(x+h)f(xh)2*h*f1(x))<br>
>         <br>
> Besides the difficulty you are having defining f, your difference equation approximating the third derivative is incorrect. It should be:<br>
> <br>
> f'''(x) = (f(x+2*h)f(x2*h)2*(f(x+h)f(xh)))/(2*h^3)<br>
> <br>
> This means that after receiving the value of x you must evaluate f at four different points, f(x2*h), f(xh), f(x+h), and f(x+2*h), and the value of f(x) is not needed. Also somewhere you need to define h.<br>
> <br>
> I am puzzled as to why you wish to distinguish between negative and nonnegative values of x. The single definition<br>
> <br>
> f = @(x) exp(x)<br>
> <br>
> will cover both cases since exp(x) = 1/(exp(x) for all x.<br>
> <br>
> Roger Stafford<br>
<br>
There is at least one other error. For example, <br>
<br>
if x <0<br>
then f(x) = 1/(exp((x)));<br>
end<br>
<br>
But, we should know that<br>
<br>
1/exp(x) == exp(x)<br>
<br>
so I'm not at all sure why one would bother to<br>
try making a piecewise function out of this.<br>
<br>
John