How can i write this primitive function ?

22 views (last 30 days)
Talia attq
Talia attq on 20 Dec 2020
Commented: Rik on 16 Dec 2021
How can i write the primitive function to this function I = x^N dx with an interval [0,1,]as an anonymous function and primitive function . AND How should I write a code that calculates exact integrals when N = 1, N = 2, N = 3, N = 4, N = 5. I wrote this kod but i do not know how to continue, iam getting an error which says "Unrecognized function or variable 'N' "
f= @(x)x^N;%function
F=@(x)(x^((N)+1))/((N)+1);% Primitive function
a =0;
b =1;
N=1:5;
exact_value = F(b)-F(a); % exact value
for i=1:length(N)
exact_value = exact_value +( F(b)-F(a));
end

Answers (2)

Alan Stevens
Alan Stevens on 20 Dec 2020
Edited: Alan Stevens on 20 Dec 2020
You need to make the f's a function of N as well. e.g:
F=@(x,N)(x^((N)+1))/((N)+1);% Primitive function
then make sure you call them with two parameters. e.g:
F(b,N)
  2 Comments
Alan Stevens
Alan Stevens on 20 Dec 2020
Edited: Alan Stevens on 20 Dec 2020
You probably want
for i=1:length(N)
exact_value(i) = F(b,N(i))-F(a,N(i));
end
Rik
Rik on 16 Dec 2021
Deleted comments:
Talia attq on 20 Dec 2020 (prior to the response by Alan):
now i wrote it this way
f= @(x,N)x.^N;%function
F=@(x,N)(x.^((N)+1))./((N)+1);% Primitive function
a =0;
b =1;
N=1:5;
exact_value = F(b,N)-F(a,N); % exact value
for i=1:length(N)
exact_value = exact_value+(F(b,N)-F(a,N));
end
but I get the wrong values on exact value, cuz i calculated the values of its by hand and I got much less values than I got when I ran my matlab code
Talia attq on 20 Dec 2020
Thank you so much for the help
Talia attq on 20 Dec 2020
I have this numerical integrate 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
if i want to calculte the same function f= @(x,N)x.^N when N = 1, N = 2, N = 3, N = 4, N = 5, using my numerical integrate func 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 thinking about to writw tow for-loops.
f= @(x,N)x.^N;%function
F=@(x,N)(x.^((N)+1))./((N)+1);% Primitive function
a =0;
b =1;
N=1:5;
n=1:2;
exact_value = F(b,N)-F(a,N); % exact value
I=integrate(f, a, b, n);
for i=1:length(N)
exact_value(i) =(F(b,N(i))-F(a,N(i)));
end
for N=1:5
I(N)=integrate(f, a, b, n);
for n=1:2
I(n)=integrate(f, a, b, n);
end
end
but when i ran it i got this error:
Error using /
Matrix dimensions must agree.
Error in integrate (line 3)
h =(b-a)/n;%interval size
Error in upp3b (line 8)
int=integrate(f, a, b, n);

Sign in to comment.


Steven Lord
Steven Lord on 20 Dec 2020
f= @(x)x^N;%function
F=@(x)(x^((N)+1))/((N)+1);% Primitive function
Either define N before these two lines or define f and F to accept N as an additional input and pass both x and N into the function when you call it.
With either approach, because N is not a scalar, you'll need to use element-wise power and division operators .^ and ./ instead of ^ and /, see this documentation page.
But if you're not writing this as part of a homework assignment and you want to compute the integral of that function I recommend calling the integral function rather than writing your own.
  2 Comments
Steven Lord
Steven Lord on 20 Dec 2020
%{
exact_value = F(b,N)-F(a,N); % exact value
for i=1:length(N)
exact_value = exact_value+(F(b,N)-F(a,N));
end
Nothing inside this for loop depends on the loop variable at all, so that code is equivalent to:
exact_value = (length(N)+1) * (F(b, N)-F(a, N));
%}
If you meant to add up all the elements of exact_value (as it was defined before the for loop) then either use sum or iterate over elements of exact_value.
exact_value = 1:5;
s1 = sum(exact_value)
s1 = 15
s2 = 0;
for whichValue = 1:numel(exact_value)
s2 = s2 + exact_value(whichValue);
end
s2
s2 = 15
Rik
Rik on 16 Dec 2021
Deleted comments:
Talia attq on 20 Dec 2020
The problem is that i am writing this as a part of assignment, i wrote it this way now
f= @(x,N)x.^N;%function
F=@(x,N)(x.^((N)+1))./((N)+1);% Primitive function
a =0;
b =1;
N=1:5;
exact_value = F(b,N)-F(a,N); % exact value
for i=1:length(N)
exact_value = exact_value+(F(b,N)-F(a,N));
end
but still it dosent work .I get the wrong values as exact value, cuz i calculated the values of its by hand and I got much less values than I got when I ran my matlab code
Talia attq on 20 Dec 2020
I have this numerical integrate 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
if i want to calculte the same function f= @(x,N)x.^N when N = 1, N = 2, N = 3, N = 4, N = 5, using my numerical integrate func 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 thinking about to writw tow for-loops.
f= @(x,N)x.^N;%function
F=@(x,N)(x.^((N)+1))./((N)+1);% Primitive function
a =0;
b =1;
N=1:5;
n=1:2;
exact_value = F(b,N)-F(a,N); % exact value
I=integrate(f, a, b, n);
for i=1:length(N)
exact_value(i) =(F(b,N(i))-F(a,N(i)));
end
for N=1:5
I(N)=integrate(f, a, b, n);
for n=1:2
I(n)=integrate(f, a, b, n);
end
end
but when i ran it i got this error:
Error using /
Matrix dimensions must agree.
Error in integrate (line 3)
h =(b-a)/n;%interval size
Error in upp3b (line 8)
int=integrate(f, a, b, n);

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!