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 个评论
Mery
Mery 2022-12-28
Can you tell me please, if the writing of the double summation in F(tvec,n,sstep, B)
its true or not ?
Jan
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
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 个评论
Mery
Mery 2022-12-28
编辑:Jan 2022-12-28
yes, is exactly the error
Error using *
Inner matrix dimensions must agree.
Error in F (line 26)
uvect = uvect + coef *(cos(ai*yvect)*cos(aj*zvect));
Jan
Jan 2022-12-28
@Mery: Okay. I've written the explanation already: The multiplication of twi [1 x n] vectors is not defined. Now you have to explain, what you expect as result.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2013a

Community Treasure Hunt

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

Start Hunting!

Translated by