English 中文(简体)
1. 获取综合病媒价值观
原标题:Get integrated vector values
  • 时间:2014-10-24 18:55:38
  •  标签:
  • matlab

我正试图整合这一功能。 我的目标是不仅使该地区具有某种距离的价值,而且获得综合课程的具体价值。

实现这一目标的一个途径是使用cum。 我想利用整体或四分五裂得出同样的结果。 因此,我很想知道是否有像cum吗?

我试图为我写一些东西,但这项工作非常缓慢,似乎比cum陷更糟。 我稍后想把测量数据结合起来。 因此,它赢得的只是一帆风顺。

我目前的法典是:

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)
最佳回答

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.

www.un.org/Depts/DGACM/index_spanish.htm

The Math

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

https://i.stack.imgur.com/7BoLp.gif” alt=“integral in terms of x”/>

Using the u-substitution:

u substitution such that the limits of integration are 0 and 1

整体成为:

“integral

<Δx>sub>n = xn - xn-1

www.un.org/Depts/DGACM/index_spanish.htm

The Code

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指出,每一组成部分在一定间隔期内必须增加以前的间隔值。

在我的机器上,这至少比 lo版要快,而且随着间隔期增加,情况会更好。

www.un.org/Depts/DGACM/index_spanish.htm

Using ode45

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);
问题回答

暂无回答




相关问题
MATLAB Solving equations problem

I want to solve these equations using MATLAB and I am sure there is a non zero solution. The equations are: 0.7071*x + 0.7071*z = x -0.5*x + 0.7071*y + 0.5*z = y -0.5*x - 0.7071*y +...

Difference between MATLAB s matrix notations

How do you read the following MATLAB codes? #1 K>> [p,d]=eig(A) // Not sure about the syntax. p = 0.5257 -0.8507 -0.8507 -0.5257 d = ...

preventing data from being freed when vector goes out of scope

Is there a way to transfer ownership of the data contained in a std::vector (pointed to by, say T*data) into another construct, preventing having "data" become a dangling pointer after the vector goes ...

Divide two polynomials using MATLAB

I want to divide p(x) by q(x) given that: p(x)=-5x^4+3x^2-6x q(x)=x^2+1 I tried: p=inline( -5*(x^4)+3*(x^2) , x ) p = Inline function: p(x) = -5*(x^4)+3*(x^2) q=inline( x^2+1 , x ) q = ...

matlab deals with Fibbonacci

The Fibonacci series is given as follows: 1, 2, 3, 5, 8, 13, 21, ... How can I write a script which calculates and prints the n-th Fibonacci term (for n>2), where n is inputed by the user. This ...

How do I plot lines between all points in a vector?

I have a vector containing some points in 2-D space. I want MATLAB to plot these points with lines drawn from every point to every other point. Basically, I want a graph with all vertices connected. ...

How do I create a string using a loop variable in MATLAB?

I have a loop like this: for i=1:no %some calculations fid = fopen( c:\out.txt , wt ); %write something to the file fclose(fid); end I want data to be written to different files like ...

热门标签