Find the variable inside the LCM argument

6 ビュー (過去 30 日間)
ali
ali 2024 年 4 月 22 日
編集済み: John D'Errico 2024 年 4 月 22 日
I came up with a B=lcm(nrt,Nr*Nc) formula how to find Nr*Nc ?? i have B and nrt

回答 (3 件)

John D'Errico
John D'Errico 2024 年 4 月 22 日
編集済み: John D'Errico 2024 年 4 月 22 日
You have the expression:
B=lcm(nrt,Nr*Nc)
where the variables B and nrt are known. The unknown variable is the product nr*nt. I'll call that product X, since it is the unknown. The solution is actually quite simple.
X = divisors(nrt)*B/nrt
Here the divisors function is part of the symbolic toolbox. This formula requires only that the ratio B/nrt is a whole number.
help sym/divisors
DIVISORS Divisors of an expression. DIVISORS(S), where S is a SYM, returns the divisors of S. If S is integer or rational, only the nonnegative divisors are returned; if S is nonconstant, only divisors free of constant factors are returned. DIVISORS(S, VARS) returns the divisors of S, such that all factors not containing vars are considered constant, and divisors containg them are omitted. Examples: divisors(sym(6)) is [1, 2, 3, 6] divisors(x^2-1) is [1, x - 1, x + 1, (x - 1) * (x + 1)] divisors(x^2*y*z^2, [x y]) is [1, x, x^2, y, x*y, x^2*y] See also SYM/FACTOR. Documentation for sym/divisors doc sym/divisors
I'll explain it below. The LCM function is a pretty simple one. It finds the least common multiple of two numbers.
B = lcm(nrt,X)
For example, what is the least common multiple of the pair (3,5)? Since 3 and 5 share no common prime divisors, then the least common multiple is just 15. That is, the least common multiple is the smallest integer that has both of the elements as factors.
lcm(3,5)
ans = 15
When the two numbers have common divisors, then the LCM is not quite as large as their product. For example, with 6 and 10, they both share a common prime factor of 2. So the lcm is not as large as 60. Instead, it is 30.
lcm(6,10)
ans = 30
This might point out an issue. The solution X for the problem you show, has X as NOT being a unique number. FOR EXAMPLE, it is true that both of these results yield the LCM:
lcm(60,42)
ans = 420
lcm(60,7)
ans = 420
Do you see that solving for X in your problem does not have a unique solution? Hmm. So, what can we do? We can solve for all possible solutions. The solution could employ the divisors function, part of the symbolic toolbox. For example, given B. Here, I'll choose B=300. If B is the LCM of two numbers, then it must be true that B is a multiple of nrt. Remember LCM stands for Least Common MULTIPLE.
B = 300;
nrt = 12;
Then the simple formula to compute ALL possible solutions for X (X is the product in your problem, so X=nr*nt) is just:
X = divisors(nrt)*B/nrt
X = 1x6
25 50 75 100 150 300
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Test it out.
lcm(nrt,X)
ans = 1x6
300 300 300 300 300 300
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
In each case, you see the LCM is 300.
Again, think about it. If B=lcm(nrt,X), then B MUST be a multiple of nrt. Surely you agree with that? But what is the other term? After we cancel out the factors of B that appear in nrt, then what remains would be any of the divisors of nrt.
This also points out that your problem will not have a solution all of the time. For example:
B = 12345; factor(B)
ans = 1x3
3 5 823
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
nrt = 35; factor(nrt)
ans = 1x2
5 7
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
A solution exists ONLY if nrt divides B evenly, so B must be a multiple of nrt. Here we would have
B/nrt
ans = 352.7143
which fails the requirement.
Hmm. I don't like how I explained this.

Hassaan
Hassaan 2024 年 4 月 22 日
編集済み: Hassaan 2024 年 4 月 22 日
% Known values of B and nrt
B = 120; % Example value for B
nrt = 8; % Example value for nrt
% Find all divisors of B
divisorsB = divisors(B);
% Array to hold possible values of Nr*Nc
possible_NrNc = [];
% Check each divisor to see if it's a valid Nr*Nc
for i = 1:length(divisorsB)
candidate = divisorsB(i);
if lcm(nrt, candidate) == B
possible_NrNc = [possible_NrNc, candidate];
end
end
% Display the possible values for Nr*Nc
disp('Possible values for Nr*Nc are:');
Possible values for Nr*Nc are:
disp(possible_NrNc);
15 30 60 120
function divs = divisors(n)
divs = [];
for i = 1:n
if mod(n, i) == 0
divs = [divs, i];
end
end
end
-----------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
It's important to note that the advice and code are based on limited information and meant for educational purposes. Users should verify and adapt the code to their specific needs, ensuring compatibility and adherence to ethical standards.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.
  1 件のコメント
John D'Errico
John D'Errico 2024 年 4 月 22 日
Note that divisors already exsits in MATLAB. It is part of the symbolic toolbox. And there is a simple direct solution for the problem.

サインインしてコメントする。


sai charan sampara
sai charan sampara 2024 年 4 月 22 日
Hi ali,
If the LCM (least common multiple) of 2 numbers is known and one of the number is known then the value of the second number is not always unique. We know that product of two numbers is equal to the product of their LCM and GCD (greatest common divisor). So the second number is given by LCM multiplied by GCD divided by the first number. In this case LCM and the first number is known.
There are two possible cases. First case is when the value of LCM divided by the first number and the first number do not have a common factor. In this case the GCD can range from 1 to first number's value, comprising of all the factors of first number in between. So the possible values for the second number in that case are LCM multiplied any factor of the first number divided by the first number. Here is an example code demonstrating case 1:
B=168; %LCM
nrt=24; % 1st Number
factors_of_nrt=[1,2,3,4,6,8,12,24]; % Factors of the first number
N=B*factors_of_nrt/nrt % initial estimated values of the second number
N = 1x8
7 14 21 28 42 56 84 168
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
N_true=0; % trivial solution
j=0;
for i =1:length(factors_of_nrt)
if(lcm(N(i),nrt)==B)
j=j+1;
N_true(j)=N(i);
end
end
N_true % All possible values for Nc*Nr as follows
N_true = 1x8
7 14 21 28 42 56 84 168
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
In second case if the value of LCM divided by first number and the first number have a common factor. In this case all the values obtained in the above method will not satisfy the above condition. Here is the example code for case 2:
B=1152; %LCM
nrt=24; % 1st Number
factors_of_nrt=[1,2,3,4,6,8,12,24]; % Factors of the first number
N=B*factors_of_nrt/nrt % initial estimated values of the second number
N = 1x8
48 96 144 192 288 384 576 1152
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
N_true=0; % trivial solution
j=0;
for i =1:length(factors_of_nrt)
if(lcm(N(i),nrt)==B)
j=j+1;
N_true(j)=N(i);
end
end
N_true % All possible values for Nc*Nr as follows
N_true = 1152
Hence the solution is not always unique. The trivial solution that always works is for "Nc*Nr" to be equal to "B" itself. The other possible solutions depend upon the values of the first number and LCM divided by first number.

カテゴリ

Help Center および File ExchangeLinear Model Identification についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by