# How do I exclude values in linspace?

28 views (last 30 days)
lea.c on 28 Feb 2023
Commented: Davide Masiello on 1 Mar 2023
Hello! I have here a code that integrates a function using the Simpson's 1/3 Rule. Supposedly, my output is the step size (h), x values in a vector, and the final value of integration (y). My question is (1) how do I display the x-values in such a way that a and b is excluded? And (2) how do I display y without using inline?
For (1): The vector should be sized at [1 17] but my vector output right now is at [1 19].
For (2): The variable y must be of data type double and I understand that I'm using a function_handle. But for this, we aren't allowed to use inline. Is there a way I can do this without using inline?
Here is what I've made so far:
function [h,x,y] = integral(a,b,n)
x0 = a:n:b;
y = @(x) (log(abs(2*x/(1-2*x)+1)));
% step size
h = (b - a)/n;
% summation of odd and even terms
odd = 0;
even = 0;
% for odd terms
for i = 1:2:n-1
x0 = a + (i*h);
odd = odd + y(x0);
end
% for even terms
for i = 2:2:n-2
x0 = a + (i*h);
even = even + y(x0);
end
% simpson 1/3 rule
s = (h/3) * (y(a) + y(b) + 4*odd + 2*even);
% values of x in vector
x = linspace(a,b,n);
end
To call the function above, I made this:
a = 1+10*rand();
b = a+30*rand();
n = 1+round(20*rand(),0);
[h,x,y] = integral(a,b,n)
And the output I get is:
h =
0.2468
x =
9.3878 9.6501 9.9123 10.1746 10.4368 10.6991 10.9614 11.2236 11.4859 11.7481 12.0104 12.2727 12.5349 12.7972 13.0594 13.3217 13.5840
y =
function_handle with value:
@(x)(log(abs(2*x/(1-2*x)+1)))
Luca Ferro on 28 Feb 2023
for the point 1), after the linpace declaration cut the extremes:
x = linspace(a,b,n);
x=x(2:end-1);

Davide Masiello on 28 Feb 2023
Edited: Davide Masiello on 28 Feb 2023
1) MatLab already implements a function called integral, so I would recommend renaming your function to something like my_integral.
2) At the moment, y seems to be the function you want to integrate rather than the final value of the integral.
3) The for loops are unnecessary.
4) It is also not necessary to define x after you have already defined x0.
5) Use dot notation for vector operation! i.e. log(abs(2*x./(1-2*x)+1))
I'd rewrite your code as below.
a = 1;
b = 10;
n = 10;
[h,x,y] = my_integral(a,b,n)
h = 0.9000
x = 1×9
1.9000 2.8000 3.7000 4.6000 5.5000 6.4000 7.3000 8.2000 9.1000
y = -16.1617
Let's now double check with the MatLab built-in function.
integral(@(x) log(abs(2*x./(1-2*x)+1)),a,b)
ans = -18.9722
The values are close but not quite the same. If we increase n and try your code again we get
n = 100;
[h,x,y] = my_integral(a,b,n)
h = 0.0900
x = 1×99
1.0900 1.1800 1.2700 1.3600 1.4500 1.5400 1.6300 1.7200 1.8100 1.9000 1.9900 2.0800 2.1700 2.2600 2.3500 2.4400 2.5300 2.6200 2.7100 2.8000 2.8900 2.9800 3.0700 3.1600 3.2500 3.3400 3.4300 3.5200 3.6100 3.7000
y = -18.7052
Which is much closer, as it should be expected. Keep increasing n and you'll get just the right answer.
function [h,x,y] = my_integral(a,b,n)
h = (b - a)/n; % step size
x = a:h:b; % Integration points
f = @(x) log(abs(2*x./(1-2*x)+1)); % Function to integrate
y = (h/3)*(f(a)+f(b)+4*sum(f(x(3:2:n-1)))+2*sum(f(x(2:2:n-1)))); % Simpson's 1/3 rule
x = x(2:end-1);
end
Davide Masiello on 1 Mar 2023
Hi @lea.c, my pleasure, glad you found a suitable solution.
If you think my answer helped, please consider accepting it for future reference to other users.