获取积分向量值 [英] Get integrated vector values

查看:85
本文介绍了获取积分向量值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试集成正弦函数.我的目标不仅是获取一定距离之间的面积值,而且要获得综合路线的具体值.

I am trying to integrate the sine-function. My goal is to get not just the value of the area inbetween a certain distance but the specific values of the integrated course.

实现此目标的一种方法是使用cumtrapz.我想使用整数或四元得到相同的结果.所以我想知道是否有像金橘?

One way to achieve this is by using cumtrapz. I want to get the same result using integral or quad. So I am wondering if there is something like cumquad?

我试图为自己写点东西,但是它工作得很慢,而且似乎比cumtrapz还差.稍后,我想整合测量数据.因此,它不会像正弦波一样简单.

I tried to write something for myself but it works very slow and seems to be even worse than cumtrapz. Later on I want to integrate measured data. So it won't be as simple as a sine.

这是我当前的代码:

a = 0; b = 10;

x = a:0.1:b; 
y = 2*sin(3*x);
pp = spline(x,y);

y2=zeros(1,length(y));
y3=zeros(1,length(y));

y2(1)=integral(@(x)ppval(pp,x),x(1),x(2));
y3(1)=integral(@(x)ppval(pp,x),x(1),x(2));
for a=2:(length(y)-1)

   y2(a) = y2(a-1)+integral(@(x)ppval(pp,x),x(a-1),x(a));
   y3(a) = y3(a-1)+quad(@(x)ppval(pp,x),x(a-1),x(a));

end
y4=cumtrapz(x,y);
% y5=cumsum(y);

plot(x,y)
hold on
plot(x,y2,'-ro')
plot(x,y3,'-kx')
plot(x,y4,'g')
syms x % compare with analytical result
ya=2*sin(3*x);
ya5=int(ya)+(2/3);
ezplot(x,ya5)

推荐答案

使用integral

我认为没有办法让MATLAB沿路径返回积分,因此您一次执行一次Δ x 的积分是正确的. 缓慢的原因是循环和每个integral调用的随后重新启动. 您可以通过将每个时间间隔的积分作为矢量值函数来避免循环.

Using integral

I don't think there is a way to have MATLAB return the integral along the path, so you are correct in performing the integration one Δx at a time. The slowness comes from the loop and subsequent restart of every integral call. You can avoid the loop by posing the integral over every interval as a vector-valued function.


假设我们将 x 划分为具有 N 个总边界的 N-1 个区间,并将区间边界表示为 x n 其中, n∈ {1,2,3 ...,N} 使得 x 1 ≤ x 2 ≤ x 3 ...≤ x N . 那么区间上的任何积分都是

Suppose we divide x into N-1 intervals with N total boundaries and denote an interval boundary as xn where n ∈{1,2,3...,N} such that x1 ≤ x2 ≤ x3 ... ≤ xN. Then any integral over the interval would be

使用 u 替代:

积分变为:

其中Δ x n = x n -x n-1

where Δxn = xn - xn-1


因此,现在,我们可以通过指定下限 x n-1 并指定间隔宽度Δ x ,并从 0 集成到 1 . 最好的部分是,如果下界和间隔宽度是向量,我们可以根据 u 创建向量值函数,并将integral与选项'ArrayValued' = true集成.

So now, we can pose the interval integration of any function by specifying the lower bound xn-1, specifying the interval width Δx, and integrating from 0 to 1. The best part is that if the lower bound and interval widths are vectors, we can create a vector-valued function in terms of u and have integral integrate with the option 'ArrayValued' = true.

x    = a:0.1:b; 
xnm1 = x(1:end-1);
dx   = x(2:end) - xnm1;
fx   = @(x) 2*sin(3*x);
f    = @(u) dx .* fx(dx*u+xnm1);
y    = cumsum([0,integral(@(u)f(u),0,1,'ArrayValued',true)]);

cumsum解释了一个事实,即给定间隔内的每个积分都需要添加前一个间隔的值.

The cumsum accounts for the fact that each integral over a given interval needs to have the value of the previous interval added to it.

在我的机器上,这至少比循环版本快几个数量级,并且随着间隔计数的增加而变得更好.

On my machine, this is at least order of magnitude faster than the loop version and gets better as the interval count increases.

使用还可以使用ode45进行集成. 它的效率几乎不及integral方法,但从概念上讲可能更容易并且看上去更干净. 实际上,当要求返回与integral 相等的绝对错误时,ode45比上述积分方法慢大约10倍.

Use can also use ode45 to perform the integration. It is not nearly as efficient as the integral method, but it may be easier conceptually and look cleaner. In fact, ode45 is about 10 times slower than the integral method above when required to return an absolute error on par with that of integral.

a    = 0;
b    = 10;

% These options are necessary to approach the accuracy of integral
opt = odeset('RelTol',100*eps(),'AbsTol',eps());
sol = ode45(@(x,y) 2*sin(3*x),[a,b],0,opt);

x    = a:0.01:b; 
yint = deval(sol,x);

这篇关于获取积分向量值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆