Question on summing function handles

조회 수: 5 (최근 30일)
Jordan Boote
Jordan Boote 2022년 3월 24일
편집: VBBV 2022년 3월 25일
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일
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일
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.

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

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by