Hello, I”ve seen a solution on here about how to calculate a derivative function of a function handle here. I would like an explanation of exactly what does this line do: f1 = eval(<" To take the derivative of a symbolic function, you have to create a function handle, which is done with the first two lines. Đang xem: Derivative.m matlab
The diff function works in different ways depending on the input. It either takes the numeric difference (shortening the vector length by 1), or calculating the derivative of a function handle. The first syntax would be diff(f(3)), while the second would be diff(f(x)).
Because you want to use the result to convert it to a function handle (a symbolic function or anonymous function), you need to convert the result from diff to the displayed text. That is what the char is doing.
Now you have a problem: your code is saved as text. Luckily there”s a function you should otherwise leave alone: eval. That function takes strings/char arrays as an input and returns the result as if that array had been typed as normal code. In this case you add the
(x) diff(f(x)); instead, but that doesn”t work because of the first syntax of diff. This line of code first calculates f(x), and then does diff. As the input to diff is just a regular scalar, it calculates the numeric difference, which reduces the length of the input by 1, resulting in an empty output. If you put in a vector, the result would be even worse, as you would get a vector response, but not what you would expect.
Thank you for the addition. I was already wondering if there really was no alternative to eval here. That did seem odd to me.
Also, some of the routines in symbolic expressions mean something different than the MATLAB routines of the same name, or need to be converted like int() needs to be converted to integral() .
If you have a symbolic expression that you need to make into a function handle to apply to numeric arguments, then use matlabFunction()
Mathematics and Optimization > Symbolic Math Toolbox > MuPAD > MuPAD Language Fundamentals > Data Types > Data Structures > Common Operations
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
Select web site
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other wnyrails.org country sites are not optimized for visits from your location.
Accelerating the pace of engineering and science
wnyrails.org is the leading developer of mathematical computing software for engineers and scientists.