MATLAB question re interpolation and approximating derivative.

In summary, for a MATLAB code that generates approximated values of a function f(x)=cos(x) with random errors and plots the error with respect to the value of h, using a logarithmic scale is necessary for visibility. The code should also simulate a calculation with a larger error to obtain an approximation for f(x+h).
  • #1
peripatein
880
0
Hi,
I'd sincerely appreciate it if someone were willing to review the few lines of MATLAB code below and indicate why they don't quite yield the expected output.

Homework Statement


I am asked to generate using MATLAB approximated values of f(x)=cos(x) at nodes x+h,x-h with random errors <=5*10-6 (using rand) for h=10-8,10-7,...,10-1. Hence,
f~(x+h)=f(x+h)+e(x+h)
f~(x-h)=f(x-h)+e(x-h)
where |e(x)|<=5*10-6
in order to then find approximation for f'(1.2) by using the approximation:
f'(x)=[f(x+h)-f(x-h)]/2h
I am finally asked to plot the error with respect to the value of h.

Below is my code. I am not really sure why it yields one line across the y-axis and another across the x axis.

Homework Equations


The Attempt at a Solution

h=(10^-1).^[1:8];
x=1.2;
fminush=cos(x-h)+(5e-6)*rand(1,1);
fplush=cos(x+h)+(5e-6)*rand(1,1);
fder=(fplush-fminush)./(2*h);
plot(h,abs(-sin(x)-fder))
 
Physics news on Phys.org
  • #2
What do you get for fder (typical values)?
Do you use a logarithmic scale for h?
 
  • #3
h was defined as a vector whose values are:
10-8,10-7,10-6,10-5,10-4,10-3,10-2,10-1
What do you mean by typical values? Are you asking which values I obtain for that parameter (viz. fder) as I execute the code?
 
  • #4
peripatein said:
h was defined as a vector whose values are:
10-8,10-7,10-6,10-5,10-4,10-3,10-2,10-1
Sure.
What do you mean by typical values? Are you asking which values I obtain for that parameter (viz. fder) as I execute the code?
Yes. Please post them, so it is easier to understand what went wrong.
I said "typical" as your code has random numbers in it, so the result won't be the same every time you run the code.
 
  • #5
fder:
2.27670626629095e-06
2.27670626684606e-05
0.000227670626684606
0.00227670626684606
0.0227670626684606
0.227670626684606
2.27670626684606
22.7670626684606
 
  • #6
Hmm, you are adding the same random value every time. That's probably not the intended way.

Apart from that: okay. Use a logarithmic scale for both h and fder.
 
  • #7
I am not sure how to go about these two. Could you kindly guide me through? I am not asking you to state precisely how it should be carried out, but elaborating a bit more would prove very helpful.
For instance, need I to reset the rand seed to solve problem #1?
For #2, did you mean I should log both sides of the equation?
What about the plotting itself? How may I correct that?
 
  • #8
For instance, need I to reset the rand seed to solve problem #1?
No, it is sufficient to let MATLAB generate 16 random numbers. You can generate them as array, or try .+ or .* in the lines where you use them.
For #2, did you mean I should log both sides of the equation?
The scale in the plot should be logarithmic.
 
  • #9
How may I change the scale in the plot to be logarithmic? Did you by any chance mean something like this?:
fminush=cos(x-h).+(5e-6)*rand(1,1);
fplush=cos(x+h).+(5e-6)*rand(1,1);
plot(log(h),log(abs(-sin(x)-fder)))
 
  • #10
That could work. Just test it? I don't have access to MATLAB currently.
If it does not work, the manual will explain it, together with many websites.
 
  • #11
Well, in that case I have a few questions, if I may, concerning what you wrote, but also regarding the task in general.
(1) Does e(x+h) differ in any way from e(x)? I mean, I understand that e(x+h) designate the error for f(x+h) whereas e(x) designate that for f(x), but do they differ in value and, mainly, in function/expression?
(2) Should e(x) be defined as a function? For instance, the algebraic expression for the remainder in a Taylor series could be used, wherein values of h should/could be substituted, or, at least, which could be limited to random number which <= 5e-6. What I am asking is basically this - should e(x) (and e(x+h)) be evaluated algebraically or does the simple, most straightforward use of rand() in this case sufficient?
(3) How could f~(x+h) be only an approximation for f(x+h) whereas the first term is f(x+h) itself?
(4) Why did you advise using a logarithmic scale and why wouldn't the plot be visible otherwise? Using a logarithmic scale was NOT part of the instructions.
(5) Below is my revised code:

h=(10^-1).^[1:8];
x=pi;
r=5e-6.*rand(16,1);
for i=1:8
fminush(i)=cos(x-h(i))+r(i);
fplush(i)=cos(x+h(i))+r(i+8);
end
fder=(fplush-fminush)./(2*h);
plot(log(h),log(abs(-sin(x)-fder)))

is there a way to avoid the loop? I have tried redefining r as (8,1) but I keep getting the error message: "Matrix dimentions must fit" or something of the sort.
(6) Here are the values I got for fder, upon executing the revised code:
4.617329096845424e-06
0.000146689415891910
-0.000499959502409109
0.00630868582096600
0.0195747882347774
-0.798172721805379
-6.79296949412933
107.411448829753

and attached is the plot. Is it actually supposed to look like that?
 

Attachments

  • Error.jpg
    Error.jpg
    10 KB · Views: 427
  • #12
peripatein said:
Well, in that case I have a few questions, if I may, concerning what you wrote, but also regarding the task in general.
(1) Does e(x+h) differ in any way from e(x)? I mean, I understand that e(x+h) designate the error for f(x+h) whereas e(x) designate that for f(x), but do they differ in value and, mainly, in function/expression?
The expression |e(x)|<c means "the function is smaller than c for every x" - it does not matter what you insert for x (as long as it is a real number). |e(x+h)|<c would have exactly the same meaning, just written in a more confusing way.

(2) Should e(x) be defined as a function?
Technically it is, but please don't go that way, this will just lead to confusion. It is some numerical error you introduce manually with the rand() (because MATLAB is more accurate than the calculation you simulate), done.

(3) How could f~(x+h) be only an approximation for f(x+h) whereas the first term is f(x+h) itself?
You simulate a calculation with a larger error, so this calculation won't be exactly f(x+h).

(4) Why did you advise using a logarithmic scale and why wouldn't the plot be visible otherwise? Using a logarithmic scale was NOT part of the instructions.
Both values vary by 8 orders of magnitude. A logarithmic plot is the only reasonable way to make them visible at the same time.
Using a logarithmic scale was NOT part of the instructions.
At some point you'll have to learn how to plot things on your own,

and attached is the plot. Is it actually supposed to look like that?
For the function you plotted and x=pi, yes.
I think the plot gets more interesting for other values of x.
 
  • #13
Thank you very much! Is there a way to avoid using a for loop? I couldn't simply do cos(x-h)+r or even cos(x-h).r. Matlab won't allow that, even if I redefined r to be of size 8 instead of 16.
 
  • #14
I would have expected that .+ works with 8 elements for r. If it does not, I don't know.
 

Related to MATLAB question re interpolation and approximating derivative.

1. What is interpolation in MATLAB?

Interpolation in MATLAB refers to the process of estimating values between known data points. It involves creating a continuous function from a set of discrete data points.

2. How do I perform interpolation in MATLAB?

To perform interpolation in MATLAB, you can use the built-in functions such as interp1 or interp2. These functions allow you to specify the method of interpolation, the data points, and the desired output points.

3. What is the purpose of interpolating data in MATLAB?

The purpose of interpolating data in MATLAB is to fill in missing data points or to obtain more accurate values between known data points. This is useful for visualizing and analyzing data that is not evenly spaced or for generating smooth curves from discrete data points.

4. Can I interpolate data in higher dimensions in MATLAB?

Yes, you can interpolate data in higher dimensions in MATLAB using the interp2 function for 2D data or the interp3 function for 3D data. These functions allow you to specify the method of interpolation and the desired output grid.

5. How can I approximate derivatives in MATLAB?

To approximate derivatives in MATLAB, you can use the diff function or the gradient function. The diff function calculates the difference between adjacent data points, while the gradient function calculates the gradient of a surface or volume.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
866
  • Engineering and Comp Sci Homework Help
Replies
1
Views
959
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Replies
64
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Differential Equations
Replies
1
Views
770
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
Replies
5
Views
883
Back
Top