How do I exclude values in linspace?
조회 수: 51 (최근 30일)
이전 댓글 표시
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
Luca Ferro
2023년 2월 28일
for the point 1), after the linpace declaration cut the extremes:
x = linspace(a,b,n);
x=x(2:end-1);
답변 (1개)
Davide Masiello
2023년 2월 28일
편집: Davide Masiello
2023년 2월 28일
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)
Let's now double check with the MatLab built-in function.
integral(@(x) log(abs(2*x./(1-2*x)+1)),a,b)
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)
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
Davide Masiello
2023년 3월 1일
If you think my answer helped, please consider accepting it for future reference to other users.
참고 항목
카테고리
Help Center 및 File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!