Finding a definite integral

9 views (last 30 days)
Sharon
Sharon on 5 Dec 2023
Moved: VBBV on 7 Dec 2023
I want to find the definite integral ,where u and kare both a 1x100 vector and .I want to use the Matlab command "integral" to do it. But to use it, I need the integrand to be a fucntion. The u and k I have are not functions. Is there any way to find this integral? Any help wil be appreciated.
  2 Comments
VBBV
VBBV on 5 Dec 2023
Moved: VBBV on 7 Dec 2023
u and k are both functions of x. But I only know the values of u and k at some points of x.
you can interpolate missing values for u and k by only limited values of x
u = randn(1, 100); % assuming all known values for x
K = randn(1, 100); % assuming all known values for x
X = linspace(0,1,101);
f = @(x) 1.5*exp(-2*(1-x));
for k = 1:length(X)-1
F(k) = integral(f,0,X(k+1));
end
S = (u+K)*F.' % integral sum
S = 1.3599
Star Strider
Star Strider on 5 Dec 2023
Moved: Star Strider on 6 Dec 2023
The vectors —
u = [0.1600 0.1600 0.1599 0.1599 0.1597 0.1596 0.1594 0.1592 ...
0.1590 0.1587 0.1584 0.1581 0.1577 0.1573 0.1569 0.1564 ...
0.1559 0.1554 0.1548 0.1542 0.1536 0.1529 0.1523 0.1515 ...
0.1508 0.1500 0.1492 0.1483 0.1475 0.1465 0.1456 0.1446 ...
0.1436 0.1426 0.1415 0.1404 0.1393 0.1381 0.1369 0.1357 ...
0.1344 0.1331 0.1318 0.1304 0.1290 0.1276 0.1261 0.1247 ...
0.1231 0.1216 0.1200 0.1184 0.1167 0.1151 0.1133 0.1116 ...
0.1098 0.1080 0.1062 0.1043 0.1024 0.1005 0.0985 0.0965 ...
0.0945 0.0924 0.0903 0.0882 0.0860 0.0838 0.0816 0.0793 ...
0.0771 0.0747 0.0724 0.0700 0.0676 0.0651 0.0627 0.0601 ...
0.0576 0.0550 0.0524 0.0498 0.0471 0.0444 0.0417 0.0389 ...
0.0361 0.0333 0.0304 0.0275 0.0246 0.0216 0.0186 0.0156 ...
0.0125 0.0095 0.0063 0.0032 0];
k = [0.7543 0.7543 0.7541 0.7537 0.7532 0.7525 0.7517 ...
0.7507 0.7496 0.7483 0.7469 0.7453 0.7436 0.7417 ...
0.7397 0.7375 0.7352 0.7328 0.7301 0.7274 0.7245 ...
0.7214 0.7182 0.7148 0.7113 0.7076 0.7038 0.6998 ...
0.6957 0.6914 0.6870 0.6824 0.6777 0.6728 0.6677 ...
0.6626 0.6572 0.6517 0.6461 0.6403 0.6343 0.6282 ...
0.6219 0.6155 0.6089 0.6022 0.5953 0.5883 0.5811 ...
0.5737 0.5662 0.5585 0.5507 0.5427 0.5346 0.5263 ...
0.5178 0.5092 0.5004 0.4915 0.4824 0.4731 0.4637 ...
0.4541 0.4444 0.4345 0.4244 0.4142 0.4038 0.3933 ...
0.3825 0.3717 0.3606 0.3494 0.3380 0.3265 0.3148 ...
0.3029 0.2909 0.2786 0.2663 0.2537 0.2410 0.2281 ...
0.2150 0.2018 0.1884 0.1748 0.1611 0.1472 0.1331 ...
0.1188 0.1044 0.0898 0.0750 0.0600 0.0449 0.0295 ...
0.0140 -0.0016 0];
.

Sign in to comment.

Accepted Answer

Torsten
Torsten on 5 Dec 2023
Edited: Torsten on 6 Dec 2023
x = linspace(0,1,101);
u = [0.1600 0.1600 0.1599 0.1599 0.1597 0.1596 0.1594 0.1592 ...
0.1590 0.1587 0.1584 0.1581 0.1577 0.1573 0.1569 0.1564 ...
0.1559 0.1554 0.1548 0.1542 0.1536 0.1529 0.1523 0.1515 ...
0.1508 0.1500 0.1492 0.1483 0.1475 0.1465 0.1456 0.1446 ...
0.1436 0.1426 0.1415 0.1404 0.1393 0.1381 0.1369 0.1357 ...
0.1344 0.1331 0.1318 0.1304 0.1290 0.1276 0.1261 0.1247 ...
0.1231 0.1216 0.1200 0.1184 0.1167 0.1151 0.1133 0.1116 ...
0.1098 0.1080 0.1062 0.1043 0.1024 0.1005 0.0985 0.0965 ...
0.0945 0.0924 0.0903 0.0882 0.0860 0.0838 0.0816 0.0793 ...
0.0771 0.0747 0.0724 0.0700 0.0676 0.0651 0.0627 0.0601 ...
0.0576 0.0550 0.0524 0.0498 0.0471 0.0444 0.0417 0.0389 ...
0.0361 0.0333 0.0304 0.0275 0.0246 0.0216 0.0186 0.0156 ...
0.0125 0.0095 0.0063 0.0032 0];
k = [0.7543 0.7543 0.7541 0.7537 0.7532 0.7525 0.7517 ...
0.7507 0.7496 0.7483 0.7469 0.7453 0.7436 0.7417 ...
0.7397 0.7375 0.7352 0.7328 0.7301 0.7274 0.7245 ...
0.7214 0.7182 0.7148 0.7113 0.7076 0.7038 0.6998 ...
0.6957 0.6914 0.6870 0.6824 0.6777 0.6728 0.6677 ...
0.6626 0.6572 0.6517 0.6461 0.6403 0.6343 0.6282 ...
0.6219 0.6155 0.6089 0.6022 0.5953 0.5883 0.5811 ...
0.5737 0.5662 0.5585 0.5507 0.5427 0.5346 0.5263 ...
0.5178 0.5092 0.5004 0.4915 0.4824 0.4731 0.4637 ...
0.4541 0.4444 0.4345 0.4244 0.4142 0.4038 0.3933 ...
0.3825 0.3717 0.3606 0.3494 0.3380 0.3265 0.3148 ...
0.3029 0.2909 0.2786 0.2663 0.2537 0.2410 0.2281 ...
0.2150 0.2018 0.1884 0.1748 0.1611 0.1472 0.1331 ...
0.1188 0.1044 0.0898 0.0750 0.0600 0.0449 0.0295 ...
0.0140 -0.0016 0];
uk = u+k;
fun = @(t)interp1(x,uk,t).*1.5.*exp(-2*(1-t));
I = integral(fun,0,1)
I = 0.2935
I2 = trapz(x,uk.*1.5.*exp(-2*(1-x)))
I2 = 0.2935
  5 Comments
Torsten
Torsten on 6 Dec 2023
Edited: Torsten on 6 Dec 2023
Thank you very much for your kind reply and providing another method to evaluate the integral. trapz is a good method to do it. I will try it. Which method has a better approximation?
If a function is only defined at discrete points, it is reasonable to use an integration method that only needs the values of the function at these discrete points. "trapz" is a method that assumes that the function behaves piecewise linear between the discrete values given. If you supply an interpolation method for the use of "integral" that approximates your discrete values in between with higher accuracy than linear interpolation, you will get a better approximation. But usually the interpolation method is quite arbitrary because you don't know in general how u+k behaves between the discrete values. Thus higher accuracy is hard to achieve in this case (and cannot be verified).
If I use ``integral" and want to separate ``interp1" and the exponential function, can I do the following?
fun1=@(t)interp1(x,uk,t);
fun2=@(t)1.5.*exp(-2*(1-t));
fun3=@(t)fun1(t).*fun2(t);
I=integral(fun3,0,1);
Yes, you can.
For interp1, do I need to discrete t?
If you use "integral", you must provide a function that is defined for all values of x in the interval [0 1]. The easiest way is to use an interpolation function, thus using "interp1". Then instead of discrete values uk(xi) at discrete points xi you now have a function uk(x) that coincides with the array uk at the discrete points, but can be used as input function to "integral".
Sharon
Sharon on 6 Dec 2023
Thank you very much for the very detailed explanation! I appreciate it!

Sign in to comment.

More Answers (2)

Chunru
Chunru on 5 Dec 2023
Assuming that u and k are constant vectors:
u = randn(1, 100);
k = randn(1, 100);
syms x
f = (u+k)*int(1.5*exp(-2*(1-x)), [0 1])
f = 
double(f)
ans = 1×100
-0.7007 -0.1411 0.8681 -0.0427 -1.0360 1.5580 -0.8510 -0.7152 -0.3153 -1.1736 0.2003 0.6063 -3.0889 0.2113 0.6936 0.1806 -0.6329 -0.9003 -0.1897 1.5372 0.3675 1.0514 1.3399 0.9247 -0.3171 -0.6689 0.2239 -1.0677 0.4734 -1.1413
  5 Comments
Chunru
Chunru on 5 Dec 2023
Then you need to define the discrete points x and the corresponding u and k.
Sharon
Sharon on 5 Dec 2023
Yes, I have x=linsapce(0,1,101) and u is
u =
Columns 1 through 8
0.1600 0.1600 0.1599 0.1599 0.1597 0.1596 0.1594 0.1592
Columns 9 through 16
0.1590 0.1587 0.1584 0.1581 0.1577 0.1573 0.1569 0.1564
Columns 17 through 24
0.1559 0.1554 0.1548 0.1542 0.1536 0.1529 0.1523 0.1515
Columns 25 through 32
0.1508 0.1500 0.1492 0.1483 0.1475 0.1465 0.1456 0.1446
Columns 33 through 40
0.1436 0.1426 0.1415 0.1404 0.1393 0.1381 0.1369 0.1357
Columns 41 through 48
0.1344 0.1331 0.1318 0.1304 0.1290 0.1276 0.1261 0.1247
Columns 49 through 56
0.1231 0.1216 0.1200 0.1184 0.1167 0.1151 0.1133 0.1116
Columns 57 through 64
0.1098 0.1080 0.1062 0.1043 0.1024 0.1005 0.0985 0.0965
Columns 65 through 72
0.0945 0.0924 0.0903 0.0882 0.0860 0.0838 0.0816 0.0793
Columns 73 through 80
0.0771 0.0747 0.0724 0.0700 0.0676 0.0651 0.0627 0.0601
Columns 81 through 88
0.0576 0.0550 0.0524 0.0498 0.0471 0.0444 0.0417 0.0389
Columns 89 through 96
0.0361 0.0333 0.0304 0.0275 0.0246 0.0216 0.0186 0.0156
Columns 97 through 101
0.0125 0.0095 0.0063 0.0032 0
and k is
k=
Columns 1 through 7
0.7543 0.7543 0.7541 0.7537 0.7532 0.7525 0.7517
Columns 8 through 14
0.7507 0.7496 0.7483 0.7469 0.7453 0.7436 0.7417
Columns 15 through 21
0.7397 0.7375 0.7352 0.7328 0.7301 0.7274 0.7245
Columns 22 through 28
0.7214 0.7182 0.7148 0.7113 0.7076 0.7038 0.6998
Columns 29 through 35
0.6957 0.6914 0.6870 0.6824 0.6777 0.6728 0.6677
Columns 36 through 42
0.6626 0.6572 0.6517 0.6461 0.6403 0.6343 0.6282
Columns 43 through 49
0.6219 0.6155 0.6089 0.6022 0.5953 0.5883 0.5811
Columns 50 through 56
0.5737 0.5662 0.5585 0.5507 0.5427 0.5346 0.5263
Columns 57 through 63
0.5178 0.5092 0.5004 0.4915 0.4824 0.4731 0.4637
Columns 64 through 70
0.4541 0.4444 0.4345 0.4244 0.4142 0.4038 0.3933
Columns 71 through 77
0.3825 0.3717 0.3606 0.3494 0.3380 0.3265 0.3148
Columns 78 through 84
0.3029 0.2909 0.2786 0.2663 0.2537 0.2410 0.2281
Columns 85 through 91
0.2150 0.2018 0.1884 0.1748 0.1611 0.1472 0.1331
Columns 92 through 98
0.1188 0.1044 0.0898 0.0750 0.0600 0.0449 0.0295
Columns 99 through 101
0.0140 -0.0016 0

Sign in to comment.


Paul
Paul on 5 Dec 2023
Edited: Paul on 5 Dec 2023
Hi Sharon,
It seems like we want to compute the following integral
syms x u(x) k(x) w(x)
I = int((u(x)+k(x))*w(x),x,0,1,'Hold',true)
I = 
w(x) has a known form
w(x) = 1.5*exp(-2*(1-x));
which can be substituded into the expression for I
I = subs(I)
I = 
Suppose for now we know the functional form of u(x) and k(x), for example
u(x) = x, k(x) = x^2
u(x) = 
x
k(x) = 
Sub those expressions into I and evaluate the integral
I = subs(I)
I = 
release(I)
ans = 
Now, compute the integral numerically, assuming those same known expressions for u(x) and k(x)
clear
w = @(x) 1.5*exp(-2*(1-x));
u = @(x) x;
k = @(x) x.^2;
integral(@(x) (u(x)+k(x)).*w(x),0,1)
ans = 0.7500
Same result.
But in this question we only have u and k evaluated at discrete set of values of x. For example
xval = linspace(0,1,100);
u = u(xval);
k = k(xval);
Now, to evaluate the integral we need to have some way to approximate u(x) and k(x) based on those vectors of points in order to evaluate the integrand. Here, I'm using interp1 with linear interplolation, but you may want to explore other options after plotting u and k and testing various options, which could be different for u(x) and k(x), to make sure the interpolated values are a a good approximation to the functions u(x) and k(x) (hopefully you have some idea what they should look like). The key is that the interpolation has to be performed in the evaluation of the integrand.
integral(@(x) (interp1(xval,u,x)+interp1(xval,k,x)).*w(x),0,1)
ans = 0.7500

Tags

Community Treasure Hunt

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

Start Hunting!