How can I integrate a function?

3 visualizaciones (últimos 30 días)
Talia attq
Talia attq el 20 de Dic. de 2020
Editada: Rik el 16 de Dic. de 2021
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 comentario
Rik
Rik el 16 de Dic. de 2021
Editada: Rik el 16 de Dic. de 2021
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.

Iniciar sesión para comentar.

Respuestas (3)

Image Analyst
Image Analyst el 20 de Dic. de 2020
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 comentario
Rik
Rik el 16 de Dic. de 2021
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);

Iniciar sesión para comentar.


Jesús Zambrano
Jesús Zambrano el 20 de Dic. de 2020
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 comentario
Rik
Rik el 16 de Dic. de 2021
Editada: Rik el 16 de Dic. de 2021
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);

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 20 de Dic. de 2020
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

Categorías

Más información sobre Solver Outputs and Iterative Display en Help Center y File Exchange.

Etiquetas

Productos


Versión

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by