An easy? Matlab Problem involving matrices

  • Thread starter Thread starter alba_ei
  • Start date Start date
  • Tags Tags
    Matlab Matrices
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB problem involving matrix operations and the computation of a specific mathematical expression. Participants explore different coding approaches to solve the problem, focusing on the use of matrices versus loops and the implications for efficiency and code clarity.

Discussion Character

  • Homework-related
  • Technical explanation
  • Mathematical reasoning

Main Points Raised

  • One participant expresses difficulty in using the element-wise division operator (./) due to matrix size mismatches and seeks guidance on using the prod function effectively.
  • Another participant suggests iterating through the values of x with a for loop as a potential solution, providing a sample code that they acknowledge is not optimal.
  • A different participant proposes using matrices instead of a for loop, presenting a more efficient code structure that utilizes the repmat function to create larger matrices for calculations.
  • One participant reiterates the matrix approach, expressing satisfaction with the improved code and reflecting on the efficiency of the method compared to their initial suggestion.

Areas of Agreement / Disagreement

Participants generally agree on the advantages of using matrix operations over loops for this problem, but there is no consensus on the best approach initially, as different methods are proposed and refined throughout the discussion.

Contextual Notes

Some limitations include potential misunderstandings regarding the use of element-wise operations and the implications of matrix dimensions on calculations. The discussion does not resolve these issues but highlights different coding strategies.

alba_ei
Messages
38
Reaction score
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
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
 
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
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 10 ·
Replies
10
Views
2K