Question on summing function handles

Hi all, I'm relatively new to MATLAB, and have been writing an algorithm for some time now. I've run into a problem where a function S(t,x) is the summation of another function. Below is the code and I'll explain more after:
t = linspace(0,2*pi,9);
x = linspace(-0.5,0.5,5);
Coords = combvec(t,x)'; % Creates the combination of all t with all x
% Finding the set N1
for i = 1:length(Coords)
if (Coords(i,2)>0)
N1(i,j) = Coords(i,j)
end
end
N1 = N1(any(N1,2),:);
This creates a vector of coordinates (t,x) which we want to use. I essentally want to find the below function using all coordinates (tj,xj) from this set N1.
I've tried writing the following but it's clearly wrong and not sure what to do: (note that fj is another function that depends only on these points inside the set N1.
for j = 1:length(N1)
tj = N1(j,1)
xj = N1(j,2)
fj = sin(tj)-sign(xj)
S1 = @(t,x) sum((-56*(1-sqrt((x-xj)^2+(t-tj)^2))^6*(35*sqrt((x-xj)^2+(t-tj)^2)^2+18*sqrt((x-xj)^2+(t-tj)^2)+3)*((xj-x)*fj+(tj-t))))
end
Any help would be highly appreciated.

2 件のコメント

VBBV
VBBV 2022 年 3 月 24 日
Note that your t and x vectors are not of same length. You need to make them equal in order to add.
t = linspace(0,2*pi,9);
x = linspace(-0.5,0.5,9);% here
Jordan Boote
Jordan Boote 2022 年 3 月 25 日
The combvec takes care of this though I think as what we want is every t value paired with all x values. So instead of having 9 coordinates we have many more.

サインインしてコメントする。

 採用された回答

VBBV
VBBV 2022 年 3 月 24 日
編集済み: VBBV 2022 年 3 月 24 日

0 投票

for j = 1:length(N1)
% remaining code here
S1 = @(t,x) ((-56.*(1-sqrt((x-xj).^2+(t-tj).^2)).^6.*(35*sqrt((x-xj).^2+(t-tj).^2).^2+18*sqrt((x-xj).^2+(t-tj).^2)+3).*((xj-x).*fj+(tj-t))))
S11(j,:) = S1(t,x);% assuming t and x are defined as earlier
end
S11 = sum(S11(:,:))
You can sum S1 outside of loop after computing it.

2 件のコメント

Jordan Boote
Jordan Boote 2022 年 3 月 25 日
Will give this a try, thank you
VBBV
VBBV 2022 年 3 月 25 日
編集済み: VBBV 2022 年 3 月 25 日
t = linspace(0,2*pi,9);
x = linspace(-0.5,0.5,9); % this is important change
Coords = combvec(t,x)' % Creates the combination of all t with all x
Coords = 81×2
0 -0.5000 0.7854 -0.5000 1.5708 -0.5000 2.3562 -0.5000 3.1416 -0.5000 3.9270 -0.5000 4.7124 -0.5000 5.4978 -0.5000 6.2832 -0.5000 0 -0.3750
% Finding the set N1
for j = 1:length(Coords)
if (Coords(j,2)>0)
N1(j,:) = Coords(j,:);
end
end
N1 = N1(any(N1,2),:);
for j = 1:length(N1)
tj = N1(j,1);
xj = N1(j,2);
fj = sin(tj)-sign(xj);
S1 = @(t,x) ((-56.*(1-sqrt((x-xj).^2+(t-tj).^2)).^6.*(35*sqrt((x-xj).^2+(t-tj).^2).^2+18*sqrt((x-xj).^2+(t-tj).^2)+3).*((xj-x).*fj+(tj-t))));
S11(j,:) = S1(t,x);% assuming t and x are defined as earlier
end
S11 = sum(S11(:,:))
S11 = 1×9
1.0e+10 * -5.4988 -1.3410 -0.2565 -0.0345 0.0006 0.0391 0.2807 1.4496 5.9341

サインインしてコメントする。

その他の回答 (1 件)

David Hill
David Hill 2022 年 3 月 24 日

0 投票

No need for for-loop
t = linspace(0,2*pi,9);
x = linspace(-0.5,0.5,5);
[T,X]=meshgrid(t,x);
fj = sin(T)-sign(X);
S = @(t,x) sum((-56*(1-sqrt((x-X).^2+(t-T).^2)).^6.*(35*sqrt((x-X).^2+(t-T).^2).^2+18*sqrt((x-X).^2+(t-T).^2)+3).*((X-x).*fj+(T-t))),'all');

1 件のコメント

Jordan Boote
Jordan Boote 2022 年 3 月 25 日
Will also try this and reply to this comment what happens. Thank you.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

製品

リリース

R2022a

質問済み:

2022 年 3 月 24 日

編集済み:

2022 年 3 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by