Numerical Integration in matlab

조회 수: 12 (최근 30일)
AVM
AVM 2020년 1월 22일
댓글: AVM 2020년 1월 27일
what is reliable way to perform a integartion numerically in matlab? I would like to get numerical result after integration of a large function .
Is that Trapezoidal rule or Simpson's rule is reliable to do that? Pl somebody tell me what is the best way for that.
Otherwise, when I use 'int()' command directly, Matlab takes very very huge time almost 5-6 hours.
  댓글 수: 4
AVM
AVM 2020년 1월 22일
@James: Thanks . Pl see this.
clc
close all
syms 'theta' 'phi' g
g=1;
thet=4;
h=[ cos(theta) 0 sin(theta)*exp(-1i*phi) 0
0 cos(theta) g sin(theta)*exp(-1i*phi)
sin(theta)*exp(1i*phi) g -cos(theta) 0
0 sin(theta)*exp(1i*phi) 0 -cos(theta) ];
a1=(exp(-phi*2i)*(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2)^(3/2))/(g*sin(theta)^2) - (exp(-phi*2i)*cos(theta)*(g^2 + cos(theta)^2 + sin(theta)^2))/(g*sin(theta)^2) + (exp(-phi*2i)*cos(theta)*(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2))/(g*sin(theta)^2) - (exp(-phi*2i)*(g^2 + cos(theta)^2 + sin(theta)^2)*(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2)^(1/2))/(g*sin(theta)^2);
b1=(exp(-phi*1i)*(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2)^(1/2))/sin(theta) + (exp(-phi*1i)*cos(theta))/sin(theta);
c1=-(exp(-phi*1i)*(cos(theta)^2 + sin(theta)^2))/(g*sin(theta)) + (exp(-phi*1i)*(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2))/(g*sin(theta));
d1=1;
m=sqrt(a1*conj(a1)+b1*conj(b1)+c1*conj(c1)+d1*conj(d1));
u=1/m*[ a1
b1
c1
d1 ];
v=diff(u,phi);
r=dot(u,v);
f(theta,g)=1i/pi*int(r,phi,0,2*pi); %% This integration taking so much time
f = simplify(f, 'Steps',500);
ffcn = matlabFunction(f);
theta = linspace(0.001,4, 30);
g = linspace(0.001,10, 30);
[Th,G] = meshgrid(theta, g);
F=ffcn(Th,G);
%max(imag(F(:)))
%min(imag(F(:)))
figure
mesh(Th, G, F)
colormap(cool)
grid on
xlabel('\bf\theta','FontSize',14)
ylabel('\bf\alpha','FontSize',14)
zlabel('\bf\itf','FontSize',14)
%% This entire process takes long time, I quit that in between.
Walter Roberson
Walter Roberson 2020년 1월 22일
편집: Walter Roberson 2020년 1월 22일
simplify( r) before doing the integration; it compacts down quite a bit. Also,
assume(phi>=0)
assume(theta>=0)
before doing the simplify()

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2020년 1월 22일
There is no reliable numeric integration method in any programming language. For any given numeric integration method, it is possible to construct a function which the numeric integration method will return an arbitrarily wrong solution.
Trapazoidal Rule and Simpson's Rule are not reliable at all.
You could consider using integral(); provide AbsTol and RelTol parameters, and provide WayPoints whenever meaningful.
You could consider using vpaintegral() with similar parameters.
  댓글 수: 23
Walter Roberson
Walter Roberson 2020년 1월 27일
편집: Walter Roberson 2020년 1월 27일
There is no faster version of symbolic simplification, except possibly some fairly small gains in performance in newer versions.
Symbolic work is done using compiled libraries that are not written in MATLAB itself, so improvements in the MATLAB Execution Engine that have been made do not improve the symbolic engine.
If you are doing numeric integration on a system that has only one free variable, then use vpaintegral() instead of int() -- but integral() will likely be faster than vpaintegral()
AVM
AVM 2020년 1월 27일
Thanks for your information.

댓글을 달려면 로그인하십시오.

추가 답변 (2개)

AVM
AVM 2020년 1월 22일
@Walter: Thanks. I am trying that
  댓글 수: 1
AVM
AVM 2020년 1월 22일
@Strider: Thanks for your reply.

댓글을 달려면 로그인하십시오.


AVM
AVM 2020년 1월 26일
@walter: How do I can normalize an eigen vector in Matlab? PL tell me what is the corresponding command for the normalization?
  댓글 수: 4
AVM
AVM 2020년 1월 27일
Thanks a lot again.
AVM
AVM 2020년 1월 27일
@walter: With this normalized command when I try to run the following progamming, it is taking so much time as near to 1 hour and the pc start to hang frequently. I had forcefully quit that. You, pl have a look on my code and suugest me what to do. I am using matlab R2018a version.
clc;clear;
syms theta phi g
%% h is 4*4 matrix
h=[ cos(theta) 0 sin(theta)*exp(-1i*phi) g
0 cos(theta) 0 sin(theta)*exp(-1i*phi)
sin(theta)*exp(1i*phi) 0 -cos(theta) 0
g sin(theta)*exp(1i*phi) 0 -cos(theta) ];
%% a1,b1,c1 and d1 are the components of first eigenvector of h; these are pasted from command plate
%a1=(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2)^(3/2)/(g*sin(theta)^2) - (cos(theta)^3 + cos(theta)*sin(theta)^2 + g^2*cos(theta))/(g*sin(theta)^2) + (cos(theta)*(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2))/(g*sin(theta)^2) - ((g^2 + cos(theta)^2 + sin(theta)^2)*(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2)^(1/2))/(g*sin(theta)^2);
%b1=-(exp(-phi*1i)*(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2)^(3/2))/sin(theta)^3 + (exp(-phi*1i)*(cos(theta)^2 + 2*sin(theta)^2 + g^2)*(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2)^(1/2))/sin(theta)^3 - (exp(-phi*1i)*cos(theta)*(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2))/sin(theta)^3 + (exp(-phi*1i)*cos(theta)*(cos(theta)^2 + 2*sin(theta)^2 + g^2))/sin(theta)^3;
%c1= -(g^2*exp(phi*1i) + exp(phi*1i)*cos(theta)^2 + exp(phi*1i)*sin(theta)^2)/(g*sin(theta)) + (exp(phi*1i)*(cos(theta)^2 + sin(theta)^2 + g^2/2 - (g*(4*sin(theta)^2 + g^2)^(1/2))/2))/(g*sin(theta));
%d1=1;
%m=sqrt(a1*conj(a1)+b1*conj(b1)+c1*conj(c1)+d1*conj(d1));
%% normalized first eigen vector of h
%u=1/m*[a1;b1;c1;d1];
%% differently
[V,L]=eig(h);
u=V(:,1)./sqrt(sum(V(:,1).^2)); %% this call aslo for normalised eigenvector of h
x=diff(u,phi);
r=dot(u,x);
assume(theta>=0);
assume(phi>=0);
r=simplify(r,'Steps',100);
f(theta,g)=1i/pi*int(r,phi,0,2*pi);
f=simplify(f,'Steps',100);
ffcn = matlabFunction(f);
theta = linspace(0.001,4, 30);
g = linspace(.001,2, 30);
[Th,G] = ndgrid(theta, g);
%F=abs(ffcn(Th,Al));
F=ffcn(Th,G);
%max(imag(F(:)))
%min(imag(F(:)))
figure
meshc(Th,G, F)
colormap(cool)
grid on
xlabel('\bf\theta','FontSize',14)
ylabel('\bf\alpha','FontSize',14)
zlabel('\bf\itf','FontSize',14)
But when I normalize the state just by activiting a1,b1, c1 and d1, then the graph is appearing withhin few min..But I wnated to avoid those large expression of a1,b1, c1 and d1. Pl suggest me what to do.
Thanks.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by