How do you solve for a Double summation with loops
Show older comments
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 Comments
Please mention, what the problem is. "it doesn't work" is not clear enough. Remember, that it is easier to solve a problem than to guess, what the problem is.
Are the default inputs really useful for this kind of program?
if nargin ==0
n =20;
sstep =1;
elseif nargin ==1
sstep =1;
end
I'd omit this fancy tricks. But if aiajroots() accepts a 3rd input B, do not overwrite it in the code.
This looks suspicious:
ai= fzero (f1 ,[i i1 ]);
aj=fzero (f1 ,[i i1 ]);
I recommend to use a standard scheme for setting spaces: Spaces around operator and after commas, separators between elements of vectors. Then similarities become more obvious:
ai = fzero(f1, [i, i1]);
aj = fzero(f1, [i, i1]); % twice?!
Creating the function handle inside the loop is not an error, but a waste of time. Move all code, which does not depend on the loop counter before the loop:
f1 = @(y) y * tan(y) - B;
Mery
on 28 Dec 2022
Mery
on 28 Dec 2022
Jan
on 28 Dec 2022
@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
Answers (1)
Jan
on 28 Dec 2022
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 Comments
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!