Help on a Function

Greetings!
I would like your help on a function I'd like to make on Matlab.
I want to make a function called R,
[Roots] = R(X,Y,eps) and this function will be returning a table with 2 columns showing the X and Y of the points where abs(Y)<eps
Thank you in advance.

4 comentarios

dpb
dpb el 30 de Nov. de 2019
OK, simple enough. What have you got so far and where, specifically did you get stuck?
Dhmhgr
Dhmhgr el 30 de Nov. de 2019
I'm new to Matlab and I saw this interesting exercise online and there is no solution given. I don't know what to do, but I'd like to know how this type of exercises are getting solved.
dpb
dpb el 30 de Nov. de 2019
I suggest reading the Getting Started tutorial sections in the online documentation...it outlines basic syntax and operations that will get you started. There are lots of examples of writing simple functions with the doc for function
Logical addressing is the trick for such questions as this...see what
eps=0.5;
x=rand(10,1);
x(x<eps)
does.
Dhmhgr
Dhmhgr el 1 de Dic. de 2019
I see, but it didn't help me.

Iniciar sesión para comentar.

Respuestas (3)

Image Analyst
Image Analyst el 1 de Dic. de 2019

0 votos

Try this (untested)
% This function will be returning a table with 2 columns showing the X and Y of the points where abs(Y)<eps
function [Roots] = R(X,Y,eps)
Roots = []; % Initialize to no values.
indexes = abs(Y) < eps;
if isempty(indexes)
% No Y satisfies the criteria.
return;
end
xSmall = reshape(X(indexes), [], 1); % Extract indexes and make sure they're in a column vector.
ySmall = reshape(Y(indexes), [], 1); % Extract indexes and make sure they're in a column vector.
Roots = table(xSmall, ySmall);
end

5 comentarios

Dhmhgr
Dhmhgr el 1 de Dic. de 2019
I forgot to say that X and Y are row vectors. Is this solution ok if the X and Y are row vectors?
I made them like that: X=linspace(-2,2)
Walter Roberson
Walter Roberson el 1 de Dic. de 2019
Yes, that code will work with row vectors. Image Analyst took care of the detail that the values passed into table() should normally be column vectors.
dpb
dpb el 1 de Dic. de 2019
Continuing w/ Walter's explanation, because if row vectors,
>> X=1:10;Y=randn(size(X));
>> table(X,Y)
ans =
1×2 table
X Y
_____________ _____________
[1x10 double] [1x10 double]
>>
you see you get a row vector for each of the two variables as the one record in the table...this probably is not what is wanted.
Dhmhgr
Dhmhgr el 1 de Dic. de 2019
So, what should the correct answer be? The first one by the person "Image Analyst"?
Image Analyst
Image Analyst el 1 de Dic. de 2019
Of course. dpb was merely showing what would happen if I didn't use reshape to turn the data into a column vector before passing in to the table() function. It was not a separate answer in itself, and I did use reshape() so I did it correctly.

Iniciar sesión para comentar.

Dhmhgr
Dhmhgr el 1 de Dic. de 2019

0 votos

??

1 comentario

Image Analyst
Image Analyst el 1 de Dic. de 2019
I didn't notice at first that we were using eps. That is probably a bad thing to use (unless it's a trick question) because no value of Y can be less than eps unless it's pure zero.
Try this (with a variable named minValue):
X = rand(1, 100);
Y = rand(1, 100);
minValue = 0.1;
Roots = R(X,Y,minValue)
% This function will be returning a table with 2 columns showing the X and Y of the points where abs(Y)<eps
function [Roots] = R(X,Y,minValue)
Roots = []; % Initialize to no values.
indexes = abs(Y) < minValue;
if isempty(indexes)
% No Y satisfies the criteria.
return;
end
xSmall = reshape(X(indexes), [], 1); % Extract indexes and make sure they're in a column vector.
ySmall = reshape(Y(indexes), [], 1); % Extract indexes and make sure they're in a column vector.
Roots = table(xSmall, ySmall);
end
You'll see
Roots =
11×2 table
xSmall ySmall
_________________ ___________________
0.970592781760616 0.0838213779969326
0.792207329559554 0.0781755287531837
0.849129305868777 0.00463422413406744
0.743132468124916 0.0844358455109103
0.489764395788231 0.0759666916908419
0.679702676853675 0.0496544303257421
0.890903252535798 0.0964545251683886
0.257508254123736 0.0597795429471558
0.243524968724989 0.0154034376515551
0.929263623187228 0.0430238016578078
0.567821640725221 0.0811257688657853

Iniciar sesión para comentar.

Dhmhgr
Dhmhgr el 2 de Dic. de 2019
Editada: Walter Roberson el 2 de Dic. de 2019

0 votos

Okay, function made correctly.
I have an other problem regarding the same exercise.
I have:
x=linspace(-2,2,100) y=x.^4-3*x.^3+x.^2+8*x
I want to give eps a number so that by calling the R function I will be able to find the points where abs(y)<eps.
What should the eps number be?

2 comentarios

Walter Roberson
Walter Roberson el 2 de Dic. de 2019
What should the eps number be?
42.
Image Analyst
Image Analyst el 2 de Dic. de 2019
eps, which remember I said must be renamed, can be anything. Whatever you want. Whatever you use, R will find the points, if any exists, or null if they don't.

Iniciar sesión para comentar.

Categorías

Más información sobre Matrices and Arrays en Centro de ayuda y File Exchange.

Etiquetas

Preguntada:

el 30 de Nov. de 2019

Comentada:

el 2 de Dic. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by