How do you solve for a Double summation with loops
4 ビュー (過去 30 日間)
古いコメントを表示
Hi everyone,
I need to calculate and plot this double sum with loops.

where
are the roots of:
here is my code but, it doesn't work I think I created the wrong roots function. maybe!
function [ uvect, yvect, zvect ]=F(tvec,n,sstep, B)
if nargin <4
sstep =1;
end
if nargin <3
n =1000;
end
if nargin <2
B =0.1;
end
if nargin ==0
tvec =[0 0.0001 0.001 0.01 0.1 1];
end
yvect = linspace(0,1,101);
zvect =linspace(0,1,101);
[ aivec, ajvec, aijvec ]=aiajroots(n,sstep,B);
for it =1: length ( tvec )
t= tvec (it);
uvect =0;
for i=1:n
ai=aivec(i);
for j=1:n
aj =ajvec(j);
aij=aijvec(j);
coef =((sin(ai)*sin(aj))/(aij*(1+(sin(ai)^2)/B)*(1+(sin(aj)^2)/B)))*exp(-aij* t);
uvect = uvect + coef *(cos(ai*yvect)*cos(aj*zvect));
end
end
end
hold off
and the roots function is
function [ aivec,ajvec, aijvec]=aiajroots(n,sstep,B)
if nargin ==0
n =20;
sstep =1;
elseif nargin ==1
sstep =1;
end
B=0.1;
k=0; i=0;
while k<n
i1=i+ sstep ;
f1=@(y) y*tan(y)-B;
if f1(i)*f1(i1) <0
k=k+1;
ai= fzero (f1 ,[i i1 ]);
aj=fzero (f1 ,[i i1 ]);
aivec(k)=ai;
ajvec(k)=aj;
aijvec(k)=ai^2+aj^2;
end
i=i1;
end
end
There must be a problem in my code but I don't know what happened. I'm a newbie, somebody please help me. thanks a lot advance!
4 件のコメント
Jan
2022 年 12 月 28 日
@Mery: Please post a copy of the complete error message. Then the readers do not have to guess, in which line the error occurs.
If ai and aj have the same values, there is no need to calculate them both.
A simplified version of your root finding method:
function [aivec, ajvec, aijvec] = aiajroots(n, sstep, B)
f = @(y) y * tan(y) - B;
k = 0;
i = 0;
aivec = zeros(1, n-1); % Pre-allocate
while k < n
i1 = i + sstep;
if f(i) * f(i1) < 0
k = k + 1;
aivec(k) = fzero(f, [i, i1]);
end
i = i1;
end
ajvec = aivec;
aijvec = aivec.^2 + ajvec.^2;
end
回答 (1 件)
Jan
2022 年 12 月 28 日
If yvect and zvect are both [1 x 101] vectors, this line must fail:
uvect = uvect + coef * (cos(ai * yvect) * cos(aj * zvect));
% ^ here
Here you multiply 2 row vectors, which is not defined mathematically.
2 件のコメント
参考
カテゴリ
Help Center および File Exchange で Logical についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!