I'm trying to write and m-file and I'm having a few problems.
1. How do I use a given value of x in functions?
Example, my function is fun(f,x). I input fun(ln(x),2) and
it only gives me the derivative with the x, 1/x. I want the
output to give me the derivative with the 2, 0.5.
This is how it looks like now:
Function out = fun(f,x)
out = diff(f,x);
2. Do any of you have any idea what function would calculate
the equation of the tangent for the given function f with a
given dot x?
A wrote:
> Hi,
>
> I'm trying to write and m-file and I'm having a few problems.
>
> 1. How do I use a given value of x in functions?
>
> Example, my function is fun(f,x). I input fun(ln(x),2) and
> it only gives me the derivative with the x, 1/x. I want the
> output to give me the derivative with the 2, 0.5.
>
> This is how it looks like now:
>
> Function out = fun(f,x)
>
> out = diff(f,x);
You would need optional symbolic toolbox to manipulate algebraic
expressions and derivatives thereof other than numerically.
diff() is a pointwise 1st-order difference approximation to the
derivative of a numerical vector, _not_ a symbolic differentiator, sorry.
> 2. Do any of you have any idea what function would calculate
> the equation of the tangent for the given function f with a
> given dot x?
In the general case would be symbolic again (iiu the question)...
Hello amutka@welho.com:
Aside of the answer by dbp, you might want to consider using
a free toolbox named ADMAT. It allows to evaluate
numerically (not analytically) any function derivative. It
also deals with vector-valued functions, which leads to
Jacobians.
To illustrate the behavior I might use this script:
>> x=deriv(17,1)
val =
17
deriv =
1
>> y=x.*x+3*log(x)
val =
297.4996
deriv =
34.1765
The first statement defines the variable x as the one which
against the derivatives should be performed. It is
initialized with 17, and a second field is specified as "1".
The second statement defines the formula to be analyzed,
which value should be stored in variable "y".
Notice that Matlab reports that y has two fields; y.val
which is the value you expect to get (17*17+3*log(17)) and
the interesting one: y.deriv, which is numerically equal to
2*17+3/17 (i.e. the exact formula for the derivative.
The technology behind this miracle is "operator
overloading", and fortunately for use we need not to know
anything about it.
To illustrate something else, if you want to use your
example you should modify it a bit:
Function out = fun(f,x)
x=deriv(x,ones(size(x)));
dummy = feval(f,x);
out=dummy.val;
If you want to test this workaround, I can provide a copy of
the ADMAT toolbox. Just ask me off the list.
Regards
Carlos
"carlos lopez" <clv2clv_00000000_@adinet.com.uy> wrote in
message <fvko8f$kam$1@fred.mathworks.com>...
>
> To illustrate something else, if you want to use your
> example you should modify it a bit:
> Function out = fun(f,x)
> x=deriv(x,ones(size(x)));
> dummy = feval(f,x);
> out=dummy.val;
>
> If you want to test this workaround, I can provide a copy of
> the ADMAT toolbox. Just ask me off the list.
> Regards
> Carlos
I don't have matlab at home so I wasn't able to try this
example out until today.
It gave me following error:
"??? Undefined function or variable 'x'."
> I don't have matlab at home so I wasn't able to try this
> example out until today.
>
> It gave me following error:
> "??? Undefined function or variable 'x'."
>
> I entered the function as >>fun(log(x),2)
Apparently you did not defined anything named as "x". What
do you expect from a statement like "fun(log(x),2)"?
BTW, are you trying to use my solution, or the
symbolic-based one? If you want to try mine you need to
install the toolbox before!
Regards
Carlos
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.