n as the natural numbers

114 visualizaciones (últimos 30 días)
Jane Smith
Jane Smith el 12 de Jun. de 2020
Editada: John D'Errico el 30 de Jul. de 2022
What command is used to enter that n is element of the natural numbers?
  2 comentarios
Image Analyst
Image Analyst el 12 de Jun. de 2020
Do you mean input()?
KSSV
KSSV el 12 de Jun. de 2020
Are you looking for fllor, ceil, fix, round. ?

Iniciar sesión para comentar.

Respuesta aceptada

Voss
Voss el 12 de Jun. de 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 comentarios
Miranda Bahtadaze
Miranda Bahtadaze el 29 de Mzo. de 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?

Iniciar sesión para comentar.

Más respuestas (1)

John D'Errico
John D'Errico el 30 de Jul. de 2022
Editada: John D'Errico el 30 de Jul. de 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
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.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by