n as the natural numbers
131 views (last 30 days)
Show older comments
What command is used to enter that n is element of the natural numbers?
2 Comments
Accepted Answer
Voss
on 12 Jun 2020
You might try something like this (if n is a scalar):
if n > 0 && floor(n) == n
% n is a natural number
else
% n is not a natural number
end
Or if n is a vector or matrix of numbers, you can see which ones are natural numbers like this:
is_natural = n > 0 & floor(n) == n;
And get just those elements of n that are natural numbers like this:
natural_n = n(is_natural);
3 Comments
Miranda Bahtadaze
on 29 Mar 2021
Using the @ operator, define a function of the following two natural numbers: f(m,n)=m*n/(m+n)^2
Who can please help with this question?
More Answers (1)
John D'Errico
on 30 Jul 2022
Edited: John D'Errico
on 30 Jul 2022
There are many ways to interpret this question. If the goal is simply to TEST to see if n is a natural number, then you might do something like:
n = pi;
if isreal(n) && (n > 0) && (rem(n,1) == 0)
% do stuff in here, that applies only when n is a natural number
else
disp('HELP! The sky is falling!')
end
If you want to essentially assert that n IS a natural number, perhaps using syms, then you might do it like this:
syms n real positive integer
That asserts that n is a real number, that it is positive, and it is an integer. So, now suppse we wish to solve the following problem:
solve((n-1)*(n+1/2),n)
Ther are two roots to that quadratic, on at n==1, and the other at n = -1/2. As you can see, solve found only the natural number solution.
In general, you cannot tell MATLAB that a variable represents an entire infinite set. So you cannot create a variable N, where N is the set of all natural numbers. There really is not much you can do with that concept anyway in a constructive, computational language like MATLAB.
Of course, there are other, more complex things you might think of in terms of natural numbers. For example, can you solve the following linear Diophantine system of equations?
syms x y z real positive integer
xy = solve(x - 2*y + 2*z == 6,3*x + y - 4*z == -7,'returnconditions',true)
So solve failed there, even though I might assert that a solution exists:
subs(x - 2*y + 2*z == 6,[x,y,z],[4 5 6])
subs(3*x + y - 4*z == -7,[x,y,z],[4 5 6])
With some effort, I could certainly program a solution for the above, and then finding all possible solutions. For example, if we did this:
[A,b] = equationsToMatrix(x - 2*y + 2*z == 6,3*x + y - 4*z == -7,[x,y,z])
Now we can use intlinprog to find a basic primitive solution.
f = [0 0 0];
LB = [1 1 1];
intcon = [1 2 3];
xyz0 = intlinprog(f,intcon,[],[],double(A),double(b),[])
Next, we know that given the rows of A, the vector given by the cross product of those rows is orthogonal to both of them, and since the rows of A are all integer, then so will be the cross product.
V = cross(A(1,:),A(2,:))
There are infinitely many solutions in this case. In fact, it can be shown that the general family of solutions will be of the form
syms t real integer
xyz = xyz0.' + t*V
xyz is a solution for ANY integer t. Clearly, as long as t is non-negative and integer, this will provide a natural number solution. So we see that
A*xyz.' == b
But just throwing the problem at solve fails, since solve is not terribly good at some things. The question in the end is what really are you asking? And of course, this question is now a couple of years old, so it is perhaps moot.
0 Comments
See Also
Categories
Find more on Linear Algebra in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!