How to find the upper limit of a complex integral

조회 수: 5 (최근 30일)
Confidential Confidential
Confidential Confidential 2020년 11월 18일
댓글: Star Strider 2020년 11월 19일
I'm trying to solve this complex integral using matlab where Fa0,Fb0,Fc0,Fd0,v0 are all constants but k1 and k2 change in a for loop by pulling from a variable set. Where I want to find x and it lies between 0 and 1. The graph I want to get is this. Some clairifcation Tvals are the temperature which k1 and k2 are calculated and the x-axis of my plot. Second k1 and k2 in the list of 106 are used together. So at the first entry of Tval is 450 the first entry of k1 and k2 would be used in equation together.
load("Tvals.mat")
kvals=load("kvals.mat")
P=506625
R=8.314
Tin=750
v0=0.019635
yca0=0.1
ycb0=0.25
ycc0=0.10
ycd0=0.05
ca0=(P/(R*Tin))*yca0
cb0=(P/(R*Tin))*ycb0
cc0=(P/(R*Tin))*ycc0
cd0=(P/(R*Tin))*ycd0
Fa0=ca0*v0
Fb0=cb0*v0
Fc0=cc0*v0
Fd0=cd0*v0
xvals=[]
for i=1:length(Tvals)
k1=kvals.K1vals(i)
k2=kvals.K2vals(i)
func=@(x)(Fa0./(k1.*Fa0.*(1 - x).*(-Fa0.*x + Fb0)./v0.^2 - k2.*(Fa0.*x + Fc0).*(Fa0.*x + Fd0)./v0.^2))
xval=fzero(func,0);
xvals=[xvals xval];
end
plot(Tvals,xvals)
end
I've also tried to integrate it first
func=@(x)(Fa0./(k1.*Fa0.*(1 - x).*(-Fa0.*x + Fb0)./v0.^2 - k2.*(Fa0.*x + Fc0).*(Fa0.*x + Fd0)./v0.^2))
intfun=@(x)integral(func,0,x)-0.5
xval=fzero(intfun,0);

채택된 답변

Star Strider
Star Strider 2020년 11월 19일
I’ve been working on this for a while.
This works, however at some point, fsolve finds undefined values at the intial point, and stops:
D1 = load('kvals.mat');
K1vals = D1.K1vals;
K2vals = D1.K2vals;
D2 = load('Tvals.mat');
Tvals = D2.Tvals;
P=506625;
R=8.314;
Tin=750;
v0=0.019635;
yca0=0.1;
ycb0=0.25;
ycc0=0.10;
ycd0=0.05;
ca0=(P/(R*Tin))*yca0;
cb0=(P/(R*Tin))*ycb0;
cc0=(P/(R*Tin))*ycc0;
cd0=(P/(R*Tin))*ycd0;
Fa0=ca0*v0;
Fb0=cb0*v0;
Fc0=cc0*v0;
Fd0=cd0*v0;
xval=zeros(1,numel(Tvals));
func=@(x,k1,k2) (Fa0./(k1.*Fa0.*(1 - x).*(-Fa0.*x + Fb0)./v0.^2 - k2.*(Fa0.*x + Fc0).*(Fa0.*x + Fd0)./v0.^2));
for k1 = 1:numel(K1vals)
for k2 = 1:numel(K2vals)
[xval(k1,k2),fval(k1,k2)] = fsolve(@(x)integral(@(x)func(x,K1vals(k1),K2vals(k2))-0.5,0,x),1000);
end
end
[K2,K1] = ndgrid(K2vals, K1vals);
T1 = table(K1(:), K2(:), xval(:), fval(:),'VariableNames',{'K1','K2','Xval','Fval'});
save('How to find the upper limit of a complex integrall_T1.mat','T1');
This is my latest attempt, I have no idea if it’s going to work, since it takes forever for the two nested loops.
Note that it is necessary to iterate through all combinations of ‘K1’ and ‘K2’, since the integral changes depending on those values. If the loops complete, ‘T1’ gets saved as a .mat file so it is only necessary to do this once.
I have no idea what ‘Tvals’ does, since it does not appear other than in the plot. (Looping through it simply produces 106 repititions of the ‘K1’ and ‘K2’ loops.)
I’ll post this now, and if the loop completes, I’ll edit this and attach the .mat file.
  댓글 수: 4
Confidential Confidential
Confidential Confidential 2020년 11월 19일
That's still close to what I'm seeking and I think I can make it work from here. Seriously thank you so much star strider I have been tearing my hair out for three days trying things to get this to work! I can't thank you enough
Star Strider
Star Strider 2020년 11월 19일
My pleasure!
If my Answer helped you solve your problem, please Accept it!
.

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

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by