Clear Filters
Clear Filters

How to write the sum of several matrices in Matlab ?

4 views (last 30 days)
We have the sequence of matrices
B(i,j,n)=2/N exp⁡(-cn^2 ) sin⁡(nx_i ) sin⁡(n y_j )
where N and n are integers ,x_i and y_ji are real numbers in the
interval [0 ,1] and c is a real parameter strictly positive.
We pose
A(c)= ∑_B(n) for n=1,....,10
How to write in Matlab the sum A(c) ?

Answers (2)

Voss
Voss on 27 Apr 2024
A = sum(B,3);
  1 Comment
Voss
Voss on 27 Apr 2024
I you don't have B already, here's how you can get it:
% your numbers go here:
N = 10;
nx = 7;
ny = 6;
x = rand(1,nx);
y = rand(1,ny);
c = rand();
n = reshape(1:N,1,1,[]);
B = 2/N.*exp(-c.*n.^2).*sin(n.*x(:)).*sin(n.*y(:).');
size(B)
ans = 1x3
7 6 10
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A = sum(B,3)
A = 7x6
0.0031 0.0035 0.0015 0.0018 0.0029 0.0033 0.0375 0.0429 0.0181 0.0225 0.0352 0.0411 0.0106 0.0121 0.0052 0.0064 0.0100 0.0116 0.0002 0.0002 0.0001 0.0001 0.0001 0.0002 0.0355 0.0406 0.0172 0.0213 0.0333 0.0389 0.0067 0.0077 0.0033 0.0041 0.0063 0.0074 0.0037 0.0043 0.0018 0.0023 0.0035 0.0041
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

Sign in to comment.


Walter Roberson
Walter Roberson on 27 Apr 2024
syms c N
num_i = 3; %adjust as needed
num_j = 4; %adjust as needed
num_n = 10;
syms x [num_i 1]
syms y [1 num_j]
x = repmat(x, 1, num_j, num_n);
y = repmat(y, num_i, 1, num_n);
n = repmat(reshape(1:num_n, 1, 1, []), num_i, num_j, 1);
B = 2/N .* exp(-c .* n) .* sin(n .* x) .* sin(n .* y);
A = sum(B, 3)
A = 
  6 Comments
Walter Roberson
Walter Roberson on 29 Apr 2024
N = 7;
i = 1:N;
x_i = (i-1/2)/N * pi;
y_i = (i-1/2)/N * pi;
syms c
num_i = length(x_i);
num_j = length(y_i);
num_n = 10;
syms x [num_i 1]
syms y [1 num_j]
xG = repmat(x, 1, num_j, num_n);
yG = repmat(y, num_i, 1, num_n);
nG = repmat(reshape(1:num_n, 1, 1, []), num_i, num_j, 1);
B = 2/N .* exp(-c .* nG) .* sin(nG .* xG) .* sin(nG .* yG);
A = sum(B, 3)
A = 
Ad = vpa(subs(subs(A, x, x_i.'), y, y_i), 5)
Ad = 

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!