Learning Matlab: Plotting Error in f'(1) Using Loglog Scale & Subplot

  • MATLAB
  • Thread starter bobsmiters
  • Start date
  • Tags
    Matlab Scale
In summary, the conversation discusses approximating the derivative of a function using forward and central divided differences. The specific example given is for the function f(x)=sinx and calculating the error as a function of h. The question also mentions using loglog scale and subplot to display the results. The main difficulty for the individual is understanding how to calculate the numerical differentials.
  • #1
bobsmiters
12
0
I am taking a Math/Cmpt class and I am useless with this program that we need to use Matlab.

Q: The derivative of a function f(x) can be approximated at the point x=x(sub)0 by a forward divided difference

f'(x(sub)0) ≈ [f(x(sub)0 + h) - f(x(sub)0)] / h

or by a central divided difference

f'(x(sub)0) ≈ [f(x(sub)0 + h) - f(x(sub)0)] / 2h

For the function f(x)= sin x, plot the error as a function of h when these divided differences are used to approximate f'(1) = cos1 for h = 10^-1, 10^-2, 10^-3, ..., 10^-16. Use loglog scale and display your results in one window using subplot.

As far and the loglog and subplot is concerned I understand what to do... it is the rest of the question that is causing a lot of difficulties. if there are any pointers on where to begin it would be much appreciated.
 
Physics news on Phys.org
  • #2
You question is still a bit broad, but I'll bite.

Firstly I don't think the central difference is defined correctly... it should be
f'(x_0) = [f(x_0+h/2) - f(x_0-h/2)] / h

You're being asked to compute the convergence of the error of forward and central differentials, by comparing to the best answer you have available.
Fortunately your function has a well known analytic differential, otherwise you would have to compare to your best numerical estimate (you will we see how this works when you do the analytical comparison).

That aside, it seems that the main part of your question is how to compute the numerical differentials. I'm not going to tell you the answer, but you need to set up a vector for h (perhaps using logspace) and calculate the formulas as written. Just remember to use ./h not /h
 
  • #3


A: Learning a new program, especially one as complex as Matlab, can be challenging. However, with some practice and understanding of the basic concepts, you will be able to use it effectively.

For this specific problem, the first step is to understand the concept of divided differences and how they can be used to approximate derivatives. The forward divided difference uses the function values at two points, while the central divided difference uses the function values at three points.

Next, you need to understand the given function f(x)= sin x and how to calculate its derivative at a specific point. In this case, we are interested in approximating f'(1) = cos1. You can use the formula provided in the question to calculate the approximate value of the derivative at x=1 for different values of h.

Now, for the plotting part, you need to use the loglog scale, which is a logarithmic scale on both the x and y axes. This is useful when dealing with very small or very large values, such as in this case where h ranges from 10^-1 to 10^-16. Using the loglog scale will help visualize the data better.

Lastly, the question asks for the results to be displayed in one window using subplot. Subplot is a function in Matlab that allows you to create multiple plots in one figure. You can use this to plot the error as a function of h for both the forward and central divided differences in one window.

To begin, you can start by writing a code to calculate the approximate values of f'(1) using both the forward and central divided differences for different values of h. Then, use the loglog and subplot functions to plot the error as a function of h for both methods. You can also add a title and labels to your plot to make it more clear and informative.

Remember, practice makes perfect when it comes to learning a new program. Don't be discouraged if it takes some time to get the hang of it, just keep practicing and seeking help when needed. Good luck!
 

Related to Learning Matlab: Plotting Error in f'(1) Using Loglog Scale & Subplot

1. What is Matlab?

Matlab is a programming language and numerical computing environment commonly used in scientific and engineering applications. It allows users to perform complex mathematical calculations, create visualizations, and analyze data.

2. How do I plot an error in f'(1) using loglog scale in Matlab?

To plot an error in f'(1) using loglog scale, you can use the "loglog" function in Matlab. First, calculate the error and store it in a variable. Then, use the "loglog" function to plot the error against the x-axis values. You can also customize the plot by adding labels, titles, and adjusting the scale.

3. What is the purpose of using subplot in Matlab?

Subplot in Matlab is used to create multiple plots in one figure. It allows you to compare and visualize multiple datasets or different aspects of the same dataset in a single figure. This can help in analyzing and understanding the relationships between different variables or data points.

4. How can I improve the appearance of my plots in Matlab?

There are several ways to improve the appearance of plots in Matlab. You can use different line styles, colors, and markers to distinguish between multiple datasets. You can also add labels, titles, and legends to make the plot more informative. Additionally, you can adjust the axes, font size, and other visual elements to make the plot more aesthetically pleasing.

5. Where can I find resources to learn more about plotting in Matlab?

There are many online resources available for learning about plotting in Matlab. The official Matlab website offers tutorials, documentation, and examples. You can also find various video tutorials and online courses on platforms like YouTube and Coursera. Additionally, there are several books and forums where you can learn from experienced users and ask for help with specific problems.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
173
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
85
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top