Clear Filters
Clear Filters

Element Wise Matrix multiplication issue (.*)

3 views (last 30 days)
EDIT: Extremely sorry about not providing the values, still very new to MATLAB and its community
B=0.5
u=2
zeta= 0.1 to 1 with an increment of 0.01
r= 0.01 to 100 with no specific increments
M=sqrt((square(1-B^2.*(r.^2))*(1+4.*((zeta*r).^2)))/square(1-((((1+u)*B^2)+1).*(r.^2))+(B^2.*(r.^4)))+4*((zeta*r).^2)*square(1-(1+u)*(B^2.*(r.^2))));
ERROR:
Incorrect dimensions for matrix multiplication. Check that the number of columns in the first matrix matches the number of rows in the second matrix. To operate on each
element of the matrix individually, use TIMES (.*) for elementwise multiplication.
  2 Comments
Dyuman Joshi
Dyuman Joshi on 17 Sep 2023
Please provide the variables used to define M.
I suspect the error occurs because you are trying to multiply two same sized vectors (zeta and r)
Dyuman Joshi
Dyuman Joshi on 17 Sep 2023
Edited: Dyuman Joshi on 17 Sep 2023
@Yuvraaj Pasumarthy, zeta and r are not compatible for matrix multiplication or element-wise multiplication.
zeta= 0.1:0.01:1;
size(zeta)
ans = 1×2
1 91
r= 0.01:100;
size(r)
ans = 1×2
1 100

Sign in to comment.

Accepted Answer

Sam Chak
Sam Chak on 17 Sep 2023
Edited: Sam Chak on 18 Sep 2023
Update: With the values of zeta, beta, and mu provided, you can plot something. You can try experimenting yourself.
% Assigning parameters
zeta = [0.05, 0.1, 0.3, 0.5, 1];
beta = 0.5;
mu = 2;
r = logspace(-2, 2, 4001);
% Setup a repetitive looping task using the for-loop method
% the task is to generate logy-logx plot 5 times, as numel(zeta) = 5
for i = 1:numel(zeta)
M = sqrt((1 + 4*(zeta(i)^2)*(r.^2)).*(1 - (beta^2)*(r.^2)).^2)./sqrt((1 - ((1 + mu)*beta^2 + 1)*r.^2 + (beta^2)*(r.^4)).^2 + 4*(zeta(i)^2)*(r.^2).*(1 - (1 + mu)*(beta^2)*(r.^2)).^2);
txt = ['\zeta = ', num2str(zeta(i))]; % for legend purposes
loglog(r, M, 'DisplayName', txt)
hold on % retain the current plot window so that next graph can be plotted
end
% Labelling stuffs
grid on % toggle grid on
hold off
ylim([1e-4 1e2]) % set y-axis limit for display purposes (same as your image)
xlabel('Frequency ratio, r')
ylabel('Vibration transmissibility, F_{2}/F_{0}')
legend show
  9 Comments
Yuvraaj Pasumarthy
Yuvraaj Pasumarthy on 18 Sep 2023
Edited: Yuvraaj Pasumarthy on 18 Sep 2023
Thank you so much for the updating naming @Sam Chak, I had a doubt. If I want graphs for different values of mu and beta, which in the code are variables that take only one constant value, how do I change that?
Sam Chak
Sam Chak on 25 Sep 2023
You are welcome! You can change the values for beta β and mu μ on Lines 3 and 4 as indicated below:
% Assigning parameters % <-- Line 1
zeta = [0.05, 0.1, 0.3, 0.5, 1]; % <-- Line 2
beta = 0.5; % <-- Line 3
mu = 2; % <-- Line 4
r = logspace(-2, 2, 4001); % <-- Line 5

Sign in to comment.

More Answers (1)

Sam Chak
Sam Chak on 25 Sep 2023
If you want to examine the effects of varying the beta values while keeping zeta and mu fixed, the structure of the code remains the same; however, you'll loop around beta instead of zeta.
% Assigning parameters
zeta = 0.5;
beta = [0.1, 0.3, 0.5, 0.7, 0.9];
mu = 2;
r = logspace(-2, 2, 4001);
% Setup a repetitive looping task using the for-loop method
% the task is to generate logy-logx plot 5 times, as numel(zeta) = 5
for i = 1:numel(beta)
M = sqrt((1 + 4*(zeta^2)*(r.^2)).*(1 - (beta(i)^2)*(r.^2)).^2)./sqrt((1 - ((1 + mu)*beta(i)^2 + 1)*r.^2 + (beta(i)^2)*(r.^4)).^2 + 4*(zeta^2)*(r.^2).*(1 - (1 + mu)*(beta(i)^2)*(r.^2)).^2);
txt = ['\beta = ', num2str(beta(i))]; % for legend purposes
loglog(r, M, 'DisplayName', txt)
hold on % retain the current plot window so that next graph can be plotted
end
% Labelling stuffs
grid on % toggle grid on
hold off
ylim([1e-4 1e2]) % set y-axis limit for display purposes (same as your image)
xlabel('Frequency ratio, r')
ylabel('Vibration transmissibility, F_{2}/F_{0}')
title('Variation of \beta while keeping \zeta and \mu fixed')
legend show

Categories

Find more on Graphics Performance in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!