n as the natural numbers

146 次查看(过去 30 天)
What command is used to enter that n is element of the natural numbers?
  2 个评论
Image Analyst
Image Analyst 2020-6-12
Do you mean input()?
KSSV
KSSV 2020-6-12
Are you looking for fllor, ceil, fix, round. ?

请先登录,再进行评论。

采纳的回答

Voss
Voss 2020-6-12
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);

更多回答(1 个)

John D'Errico
John D'Errico 2022-7-30
编辑:John D'Errico 2022-7-30
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
HELP! The sky is falling!
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)
ans = 
1
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)
xy = struct with fields:
x: [0×1 sym] y: [0×1 sym] parameters: [1×0 sym] conditions: [0×1 sym]
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])
ans = 
subs(3*x + y - 4*z == -7,[x,y,z],[4 5 6])
ans = 
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])
A = 
b = 
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),[])
LP: Optimal objective value is 0.000000. Heuristics: Found 1 solution using diving. Upper bound is 0.000000. Relative gap is 0.00%. Optimal solution found. Intlinprog stopped at the root node because the objective value is within a gap tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default value). The intcon variables are integer within tolerance, options.IntegerTolerance = 1e-05 (the default value).
xyz0 = 3×1
4.0000 5.0000 6.0000
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,:))
V = 
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 = 
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
ans = 
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.

类别

Help CenterFile Exchange 中查找有关 Linear Algebra 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by