How do I exclude values in linspace?

63 Ansichten (letzte 30 Tage)
lea.c
lea.c am 28 Feb. 2023
Kommentiert: Davide Masiello am 1 Mär. 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)))
  1 Kommentar
Luca Ferro
Luca Ferro am 28 Feb. 2023
for the point 1), after the linpace declaration cut the extremes:
x = linspace(a,b,n);
x=x(2:end-1);

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Davide Masiello
Davide Masiello am 28 Feb. 2023
Bearbeitet: Davide Masiello am 28 Feb. 2023
Hi @lea.c, I have several comments about your code.
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
  4 Kommentare
lea.c
lea.c am 28 Feb. 2023
Thank you for your help, Davide! I have found a way to make it work. This is the working code that I have made:
function [h,x,y,a,b,n] = integral(a,b,n)
% step size
h = (b - a)/n;
% x-values
xi = 0;
xf = 0;
% vectors/array
x = a:h:b;
x(end) = [];
x(1) = [];
% for odd and even terms
x_odd = x(1:2:end);
x_even = x(2:2:length(x)-1);
x = [x_odd x_even];
% summation of odd terms
for i = 1:length(x_odd)
syi = f(x_odd(i));
xi = xi + syi;
end
% summation of even terms
for i = 1:length(x_even)
syf = f(x_even(i));
xf = xf + syf;
end
% integral and simpson's 1/3 rule
sum1 = f(x_odd);
sum2 = f(x_even);
y = (h/3)*(f(a) + f(b) + 4*sum1 + 2*sum2);
function y=f(x)
y=log(abs(2*x/(1-2*x)+1));
end
end
Davide Masiello
Davide Masiello am 1 Mär. 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.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Mathematics finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by