Element Wise Matrix multiplication issue (.*)

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 个评论

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)
@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

请先登录,再进行评论。

 采纳的回答

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 个评论

zeta= 0.1 to 1 with an increment of 0.01
r= 0.01 to 100 with no specific increments, but any increments can be given
Clearly both are not of the same size, but how do I recitfy this? I am replicating the equation of a paper for which these were the assumed values, and "M" was the equation, so how do I make the changes and write a solver for this
"but how do I recitfy this?"
Make them the same size.
"I am replicating the equation of a paper for which these were the assumed values, and "M" was the equation, so how do I make the changes and write a solver for this "
Can you post the equation you are trying to replicate?
Take a look at my updated answer above. If you find the solution helpful, please consider clicking 'Accept' ✔ on the answer and voting 👍 for it. Thanks a bunch! By the way, you should also vote for other helpful answers as tokens of appreciation.
Thanks a bunch for this, This checks out, since I am still new to this software, I have a lot of doubts about the code, so ill try to understand this and please do help me out if have other doubts about it
@Yuvraaj Pasumarthy - Consider taking the free introductory course - MATLAB Onramp Tutorial to learn the essentials of MATLAB.
I have added some comments to help you better understand the code.
Also, please consider @Dyuman Joshi's advice and take the free MATLAB Onramp Tutorial. By the way, as mentioned by @Cris LaPierre, I wouldn't have been able to understand what you really wanted if you hadn't posted an image of the plot earlier. Fortunately, you followed your instincts and posted it.
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?
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

请先登录,再进行评论。

更多回答(1 个)

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

类别

帮助中心File Exchange 中查找有关 Entering Commands 的更多信息

产品

版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by