An easy? Matlab Problem involving matrices

In summary, The conversation discusses using the prod function without encountering issues when using ./ with different matrices. The solution involves using matrices instead of a for loop and creating big matrices to calculate Sn, Sinf, and eN. This method is more efficient than using a for loop.
  • #1
alba_ei
39
1

Homework Statement


2ztdb12.png


The Attempt at a Solution


%2.11
x=1:0.5:5; a=sqrt(2.8);
n=[1:1:100];
Sn=prod(1-(x.^2)./(n.^2-a^2));
S_inf=(a/sqrt(a^2+x.^2)).*sin(pi*sqrt(a^2+x.^2))/sin(pi*a);
e_n=100*(Sn-S_inf)./S_inf

I know I can't use ./ if the two matrices are different, meaning x./n.
How do I use the prod function without going through this issue?
 
Physics news on Phys.org
  • #2
Your best bet is to iterate through the Sn with x.

I've written a bit of code here, its the worst code possible, a programmer might murder me if they would get a look at this but it solves the problem.

Now go and write clean code;


%2.11
clear all;
clc;
a=sqrt(2.8);
n=1:1:100;
X=1:0.5:5;
i=1;

for x=1:0.5:5
Sn(i) = prod(1-(x^2./(n.^2-a^2)));
i=i+1;
end

S_inf=(a./sqrt(a^2+X.^2)).*(sin(pi*sqrt(a^2+X.^2))./sin(pi*a));

e_n=((Sn-S_inf)./S_inf)*100
 
  • #3
Your best bet is to use matrices instead of a for loop.

Code:
% define input
N = 100;
x = 1:0.5:5;
a = sqrt(2.8);

% Create big matrices
X = repmat(x, [N,1]);
n = repmat( (1:N).' ,[1,length(x)]);

% Calculate Sn
SN = prod(1-X.^2./(n.^2-a^2));

% Calculate inf values
Sinf = a./sqrt(a^2+x.^2).*sin(pi()*sqrt(a^2+x.^2))/sin(pi()*a);

% Caclulate eN
eN = 100*(SN-Sinf)./Sinf
 
  • #4
RoshanBBQ said:
Your best bet is to use matrices instead of a for loop.

Code:
% define input
N = 100;
x = 1:0.5:5;
a = sqrt(2.8);

% Create big matrices
X = repmat(x, [N,1]);
n = repmat( (1:N).' ,[1,length(x)]);

% Calculate Sn
SN = prod(1-X.^2./(n.^2-a^2));

% Calculate inf values
Sinf = a./sqrt(a^2+x.^2).*sin(pi()*sqrt(a^2+x.^2))/sin(pi()*a);

% Caclulate eN
eN = 100*(SN-Sinf)./Sinf

Yess! This is much nicer. I also feel that I have to apologize for my 'best bet' part.
I just thought that less computation time would be more efficient.
 
  • #5


I would suggest using the dot product function (.*), which performs element-wise multiplication on two matrices of the same size. This would allow you to use the prod function without encountering any issues with different matrix sizes. Additionally, you can also use the colon operator to create a vector of values for n, rather than specifying it as a matrix. This would also ensure that the dimensions of the matrices are compatible for the dot product function.
 

FAQ: An easy? Matlab Problem involving matrices

What is a matrix in Matlab?

A matrix in Matlab is a rectangular array of numbers, symbols, or expressions arranged in rows and columns. It is a fundamental data structure used for storing and manipulating data in Matlab.

2. How do I create a matrix in Matlab?

To create a matrix in Matlab, you can use the command "matrix = [row1; row2; ...; rowN]", where each row is separated by a semicolon and the number of elements in each row must be the same. Alternatively, you can also use the "reshape" command to convert a vector into a matrix.

3. How can I perform operations on matrices in Matlab?

Matlab has a wide range of built-in functions for performing operations on matrices, such as addition, subtraction, multiplication, division, and others. These operations can be performed using the standard arithmetic operators (+, -, *, /), or by using specific functions like "plus", "minus", "times", and "divide".

4. Can I combine matrices of different sizes in Matlab?

Yes, you can combine matrices of different sizes in Matlab, as long as they have compatible dimensions for the operation you want to perform. For example, you can add a 2x3 matrix to a 2x3 matrix, but you cannot add a 2x3 matrix to a 3x2 matrix.

5. How do I access specific elements in a matrix in Matlab?

You can access specific elements in a matrix in Matlab by using their row and column indices. For example, to access the element in the first row and second column of a matrix "A", you would use "A(1,2)". You can also use ranges of indices to access multiple elements at once, such as "A(1:3, 2:4)" to access all elements in rows 1 to 3 and columns 2 to 4.

Similar threads

Replies
3
Views
1K
Replies
3
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
1
Views
1K
Replies
10
Views
2K
Replies
6
Views
4K
Back
Top