Integrating the following function using trapz method, while choosing a good step value (h = b-a/n ) to get enough accuracy and a fast result.

2 views (last 30 days)
Hi guys,
So I am still a newbie at matlab, and I wrote this following code. But I am not sure on the step value I should pick for an accurate and fast result when integrating. Any help would be great, I have been tyring to figure it out for some time now. Thanks!
x = linspace(0,1,0.2);
y = (((sin(x)).^2)./x)-(x.*exp(2))-4;
z = trapz(x, y);
display(z)

Accepted Answer

Star Strider
Star Strider on 21 May 2016
Please read the documentation on linspace thoroughly.
That should tell you the reason this call to it:
x = linspace(0,1,0.2);
isn’t doing what you believe it is doing.
Choosing the lower limit of ‘x’ to be eps rather than 0 might be worth considering. (You might also want to peruse the documentation for eps.)

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!