How can I integrate a function?

3 次查看(过去 30 天)
Talia attq
Talia attq 2020-12-20
编辑: Rik 2021-12-16
I have this numerical integration function:
function I = integrate(f, a, b, n)
h =(b-a)/n;%interval size
I=0;
for i=1:n %loop for the rest of the calculations
p1 =(a+((i-1)*h)) + (h/4) ;
m = ((a+((i-1)*h)) + (a+(i*h)))/2;
p2 =(a+(i*h))- (h/4);
I = I + (h/3)*((2*f(p1))-f(m)+(2*f(p2)));
end
end
I want to calculate the function f= @(x,N)x.^N when N = 1, N = 2, N = 3, N = 4, N = 5, using my numerical integration function I = integrate(f, a, b, n) . Two times : first when number of subintervals n=1, and the second time when n=2. How can I do this? I'm thinking about writing two for-loops.
x=a:1:b;
a =0;
b =1;
N=1:5;
n=1:2;
f= @(x,N)x.^N;%function
F=@(x,N)(x.^((N)+1))./((N)+1);% Primitive function
exact_value = F(b,N)-F(a,N); % exact value
int(N) = integrate(f(N),a,b, n);
for i=1:length(N)
exact_value(i) =(F(b,N(i))-F(a,N(i)));
end
for j=1:length(N)
int(j) = integrate (f(j),a,b,n);
for k=1:length(n)
int(k)=integrate(f, a, b,n(k));
end
end
but when I ran it I got this error:
Not enough input arguments.
Error in upp3b>@(x,N)x.^N (line 6)
f= @(x,N)x.^N;%function
Error in upp3b (line 13)
int(N) = integrate(f(N),a,b, n);
  1 个评论
Rik
Rik 2021-12-16
编辑:Rik 2021-12-16
I recovered the removed content from the Google cache (something which anyone can do). Editing away your question is very rude. Someone spent time reading your question, understanding your issue, figuring out the solution, and writing an answer. Now you repay that kindness by ensuring that the next person with a similar question can't benefit from this answer.

请先登录,再进行评论。

回答(3 个)

Image Analyst
Image Analyst 2020-12-20
You need to get the current value of N out of the N vector:
for j = 1 : length(N)
thisN = N(j); % Extract this particular value of N
int(j) = integrate (f(thisN), a, b, n);
for k = 1 : length(n)
int(k)=integrate(f, a, b, n(k));
end
end
  1 个评论
Rik
Rik 2021-12-16
Deleted comment:
a =0;
b =1;
N=1:5;
n=1:2;
f= @(x,N)x.^N;%function
F=@(x,N)(x.^((N)+1))./((N)+1);% Primitive function
exact_value = F(b,N)-F(a,N); % exact value
int= integrate(f(x,N),a,b, n);
for i=1:length(N)
exact_value(i) =(F(b,N(i))-F(a,N(i)));
end
for j=1:length(N)
z = N(j); % Extract this particular value of N
int(j) = integrate (f(x,z),a,b,n);
for k=1:length(n)
int(k)=integrate(f(x,N),a,b,n(k));
end
end
i ran it and got this error :
Error using .^
Matrix dimensions must agree.
Error in upp3b>@(x,N)x.^N (line 5)
f= @(x,N)x.^N;%function
Error in upp3b (line 12)
int= integrate(f(x,N),a,b, n);

请先登录,再进行评论。


Jesús Zambrano
Jesús Zambrano 2020-12-20
You have defined 'f' as a function of x and N, but in the second for-loop and in the function 'integrate' you refer to 'f' with one argument instead of two.
  1 个评论
Rik
Rik 2021-12-16
编辑:Rik 2021-12-16
Deleted comment:
a =0;
b =1;
N=1:5;
n=1:2;
f= @(x,N)x.^N;%function
F=@(x,N)(x.^((N)+1))./((N)+1);% Primitive function
exact_value = F(b,N)-F(a,N); % exact value
int= integrate(f(x,N),a,b, n);
for i=1:length(N)
exact_value(i) =(F(b,N(i))-F(a,N(i)));
end
for j=1:length(N)
z = N(j); % Extract this particular value of N
int(j) = integrate (f(x,z),a,b,n);
for k=1:length(n)
int(k)=integrate(f(x,N),a,b,n(k));
end
end
i ran it and got this error :
Error using .^
Matrix dimensions must agree.
Error in upp3b>@(x,N)x.^N (line 5)
f= @(x,N)x.^N;%function
Error in upp3b (line 12)
int= integrate(f(x,N),a,b, n);

请先登录,再进行评论。


Image Analyst
Image Analyst 2020-12-20
OK, not really sure what you're after but at least this runs without error. Put it all in one m-file.
% Initialization steps. Brute force cleanup of everything currently existing to start with a clean slate.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
a =0;
b =1;
x=a:1:b;
N=1:5;
n=1:2;
f = @(x,N) x.^N; % function
F = @(x,N) (x.^((N)+1))./((N)+1);% Primitive function
exact_value1 = F(b,N) - F(a,N); % exact value
% Looping way of doing the same thing.
for k = 1 : length(N)
exact_value2(k) = F(b,N(k)) - F(a,N(k));
end
% int(N) = integrate(@f, a, b, N); % Won't work because N is a vector.
for j=1:length(N)
thisN = N(j);
int1(j) = integrate (f, a, b, thisN);
for k=1:length(n)
int2(k)=integrate(f, a, b, n(k));
end
end
% Show values in the command window.
int1
int2
%=========================================================================
function I = integrate(f, a, b, n)
h = (b - a) / n; % interval size
I = 0;
for i = 1 : n % loop for the rest of the calculations
p1 =(a+((i-1)*h)) + (h/4) ;
m = ((a+((i-1)*h)) + (a+(i*h)))/2;
p2 =(a+(i*h))- (h/4);
I = I + (h/3)*((2 * f(p1, n)) - f(m, n) + (2 * f(p2, n)));
end
end

类别

Help CenterFile Exchange 中查找有关 Solver Outputs and Iterative Display 的更多信息

标签

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by