Differentiation in Script Matlab

In summary, if you want your script to perform differentiation on a user-defined function, you'll need to tokenize the input and apply the appropriate differentiation pattern.
  • #1
c299792458
71
0
How might I make my script perform differentiation on a user-defined function? I also want the function and its derivative (in the form of a function) to be applied throughout the script, say in the forms of f and f' respectively, which I believe requires str2func.

Example: The user is prompted for an f(x). He enters x^3. Then both x^3 and 3*x^2 are applied in the place of f and f' throughout the script.

Thanks in advance!
 
Physics news on Phys.org
  • #2
c299792458 said:
How might I make my script perform differentiation on a user-defined function? I also want the function and its derivative (in the form of a function) to be applied throughout the script, say in the forms of f and f' respectively, which I believe requires str2func.

Example: The user is prompted for an f(x). He enters x^3. Then both x^3 and 3*x^2 are applied in the place of f and f' throughout the script.

Thanks in advance!

See my answer to your related question in the Math Software sub-forum.

Unless you are using the "symbolic" features, the only options you have for the derivative is to either make the user enter both the function and the derivative or else you can approximate the derivative numerically as (f(x+h) - f(x))/h for some suitably small h.

BTW f' is not a suitable name for a variable in matlab. You'll have to choose something else to denote the derivative (df or fd for example).
 
  • #3
thanks! :)
 
  • #4
You could write yourself a really, really basic differentiation function / script in MATLAB, but you'd have to do tokenizing to break down the entry and then apply the appropriate differentiation pattern.

But if you're looking to do a lot of symbolic math, you're better off with an actual symbolic math package (e.g. Mathematica, Maple, or the Symbolic Math Toolbox for MATLAB). There's also a few open-source / freeware ones available (like Maxima or Axiom, both with various add-on GUIs):
http://en.wikipedia.org/wiki/Comparison_of_computer_algebra_systems
 
  • #5


There are a few different ways you could approach this problem in Matlab. One option would be to use the "diff" function, which calculates the derivative of a user-defined function. You could prompt the user for the function and then use "diff" to calculate the derivative, storing both the original function and its derivative as variables. These variables could then be applied throughout the rest of your script.

Another option would be to use the Symbolic Math Toolbox in Matlab, which allows you to create symbolic functions and perform symbolic differentiation. This would involve creating a symbolic function using the "sym" function, prompting the user for the function, and then using the "diff" function to calculate the derivative. You could then use the "subs" function to substitute in the original function and its derivative throughout your script.

In both cases, you may need to use "str2func" to convert the user's input into a function that can be used in your script. This function takes a string as input and returns a function handle that can be used in your calculations.

I hope this helps guide you in the right direction. Good luck with your script!
 

1. What is differentiation in Matlab?

Differentiation in Matlab refers to the process of finding the derivative of a function or mathematical expression. It is a fundamental mathematical operation that calculates the rate of change of a function with respect to its independent variable.

2. How do I perform differentiation in Matlab?

To perform differentiation in Matlab, you can use the built-in function "diff". For example, if you have a function f(x) and you want to find its derivative with respect to x, you can use the command "diff(f,x)". This will return the derivative of the function as a new function.

3. Can Matlab differentiate symbolic expressions?

Yes, Matlab can differentiate symbolic expressions using the "diff" function. However, in order to do so, you need to first define the symbolic variables using the "syms" command.

4. Is there a way to visualize the results of differentiation in Matlab?

Yes, Matlab has a built-in function called "ezplot" that can be used to plot the derivative of a function. You can specify the range of values for the independent variable and the function will be plotted along with its derivative.

5. What is the importance of differentiation in Matlab?

Differentiation is a crucial mathematical operation in engineering, physics, and other scientific fields. In Matlab, differentiation is used to calculate rates of change, find critical points, and solve optimization problems. It is also essential in graphing functions and analyzing their behavior.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
997
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
Back
Top