Thread Subject: Help with compiling a Derivative m-file

Subject: Help with compiling a Derivative m-file

From: A

Date: 29 Apr, 2008 14:07:21

Message: 1 of 5

Hi to all!

I really need some help with a m-file I'm working on. I know
some math but I'm a total noob with matlab.

I need to write an m-file that does the following operations
to a given function.
-given function = f
-given dot = x
-given spacing = dx

The function that would do all of the operations is named as
fun.

1. The fun function will calculate the derivative of any
given function f in any given dot x.

2. The fun will also calculate the differential of the given
function f in the given dot x, both a estimate and the
accurate value with the given spacing dx.

3. This is tough one. The function fun will calculate the
equation of the tangent for the given function f with the
given dot x.

Here's what I've done so far:

function out = fun(f,x,dx)

out = diff(f,x); %The derivative of the function.

out = (diff(f,x)*dx %The estimated value of the differential.



Problems I have with the m-file:
-How is i possible to get multiple out's from one m-file?
-How can I substitute the x with the given value of the x?

All tips are very welcome. Cheers!

Subject: Help with compiling a Derivative m-file

From: Matt Carroll

Date: 29 Apr, 2008 14:45:05

Message: 2 of 5

"A " <amutka@welho.com> wrote in message
<fv7a2p$7ap$1@fred.mathworks.com>...
> Hi to all!
>
> I really need some help with a m-file I'm working on. I
know
> some math but I'm a total noob with matlab.
>
> I need to write an m-file that does the following
operations
> to a given function.
> -given function = f
> -given dot = x
> -given spacing = dx
>
> The function that would do all of the operations is named
as
> fun.
>
> 1. The fun function will calculate the derivative of any
> given function f in any given dot x.
>
> 2. The fun will also calculate the differential of the
given
> function f in the given dot x, both a estimate and the
> accurate value with the given spacing dx.
>
> 3. This is tough one. The function fun will calculate the
> equation of the tangent for the given function f with the
> given dot x.
>
> Here's what I've done so far:
>
> function out = fun(f,x,dx)
>
> out = diff(f,x); %The derivative of the function.
>
> out = (diff(f,x)*dx %The estimated value of the
differential.
>
>
>
> Problems I have with the m-file:
> -How is i possible to get multiple out's from one m-file?
> -How can I substitute the x with the given value of the x?
>
> All tips are very welcome. Cheers!


I am new as well to Matlab but I think you may have to
create a script file if you want multiple outputs for the
function file.

Subject: Help with compiling a Derivative m-file

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 29 Apr, 2008 15:06:30

Message: 3 of 5

In article <fv7c9h$ooh$1@fred.mathworks.com>,
Matt Carroll <matthew-carroll@utc.edu> wrote:

>I am new as well to Matlab but I think you may have to
>create a script file if you want multiple outputs for the
>function file.

No, that isn't the case. To have multiple outputs for a function,
list all the outputs on the left hand side before the '=' . For example,

function [x y] = rotate45(oldx, oldy)


You would call upon this by (for example):

[newx, newy] = rotate45(17.3, 12.8);

--
  "All is vanity." -- Ecclesiastes

Subject: Help with compiling a Derivative m-file

From: A

Date: 29 Apr, 2008 15:33:02

Message: 4 of 5

roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson) wrote in
message <fv7dhm$rm9$1@canopus.cc.umanitoba.ca>...
> In article <fv7c9h$ooh$1@fred.mathworks.com>,
> Matt Carroll <matthew-carroll@utc.edu> wrote:
>
> >I am new as well to Matlab but I think you may have to
> >create a script file if you want multiple outputs for the
> >function file.
>
> No, that isn't the case. To have multiple outputs for a
function,
> list all the outputs on the left hand side before the '='
. For example,
>
> function [x y] = rotate45(oldx, oldy)
>
>
> You would call upon this by (for example):
>
> [newx, newy] = rotate45(17.3, 12.8);
>
> --
> "All is vanity." --
Ecclesiastes

fun(f,x,dx)

f(x)=ln(x), x=2, dx=0.01 %these the given values

f?(x)=1/x %the derivative of the function = output1

f'(2)=1/2 %the derivative of the function with the given
value of 2 = output2

df1=f?(x)*dx %the estimated value of the differential

df1=0.0050 %is this = output3

df2=f(x+dx)-f(x) %the exact value of the differential

df2=0.004987 %is this = output4

Could someone give me an example how I could compile this to
a m.file in matlab? I know how to do it on paper but I have
no idea how to do it as an m.file in matlab.


Subject: Help with compiling a Derivative m-file

From: A

Date: 29 Apr, 2008 15:35:03

Message: 5 of 5

fun(f,x,dx)

f(x)=ln(x), x=2, dx=0.01 %these the given values

f?(x)=1/x %the derivative of the function = output1

f'(2)=1/2 %the derivative of the function with the given
value of 2 = output2

df1=f?(x)*dx %the estimated value of the differential

df1=0.0050 %is this = output3

df2=f(x+dx)-f(x) %the exact value of the differential

df2=0.004987 %is this = output4

Could someone give me an example how I could compile this to
a m.file in matlab?


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

Public Submission Policy

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Disclaimer prior to use.

Contact us at files@mathworks.com