Help with FOR loops
Mostrar comentarios más antiguos
Hi! I'm having trouble figuring out how to make a FOR loop for my code. I'm using the below code to calculate the area under a given curve (x and y are data provided both 100 samples) This part works just fine.
x1=0:0.01:10; %(produces 1000 samples)
y1=interp1(x,y,x1, 'spline');
area1 = trapz(x1,y1); %(produces area under the curve)
What I would like to do is change it into a FOR loop so that I can determine the minimal data density required to yield an accurate result. I can't figure how to create a FOR loop for this. I know that I can do this by changing the number of samples I take. For example by altering x1 = 0:0.001:10 I would get 10,000 samples producing a more accurate result. Instead of changing the x1 vector manually I'd like to write a for loop to do if for me so that x1=0:v:10 where v=0:0.001:0.1
Thanks!
1 comentario
Walter Roberson
el 11 de Feb. de 2018
How do you know if the result is "accurate"? Are you given external information about what the "correct" area is?
Respuestas (2)
Emily Weidensee
el 12 de Feb. de 2018
0 votos
Walter Roberson
el 12 de Feb. de 2018
0 votos
You indicate that you are just given random data. Given any finite set of data specified to finite precision, there are Aleph One (the infinity of continuous numbers) different functions that pass exactly through that data (to within round-off error.) I have given a proof by construction of this in the path -- it involves gcd() of the differences in x coordinates and involves periodic functions.
Because there is no possible way to decide between that continuous infinity of functions as being the "true" function represented by the data, there is no way to know, just given the data, what an "accurate" assessment of the area under the curve is. No matter what sampling interval you choose, it is possible to construct a function for which the answer given by trapz is exact (to within round-off error) and for which the answer would be incorrect for any other regular sampling interval used with trapz.
Interpolating data to try to get a more "accurate" area is the wrong thing to do, except in the very narrow circumstance that the interpolation function happens to be exactly the same as the true function (in which case the interpolated data is actual data.)
Categorías
Más información sobre Numerical Integration and Differentiation en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!