find intervals with just one root of function

• May 29th 2009, 05:18 AM
Babs310788
find intervals with just one root of function
hello,
I have to write a script that will give me a matrix M, with the intervals of x-values of a function, and every interval has JUST ONE root of the function:
every row of M has 2 elements (so M has 2 columns)
the elements are the first and last number of the interval, so the first column of M has the numbers of the second column of M, only one row lower.
The numbers has to be integers!!!:
for example:
y = x^3 - 9x + 0.1, that script should give me
M = [-2 1;1 4]
i have started a script like this:

xt = test_fun([a:b]) is a reference to another script I already made, that just gives me a vector xt with the y-values of a function
the function i have to work with
y = x0*cos((sqrt(4*m*k - b.^2))*t/(2*m)).*exp(-b*t/(2*m)),
but that shouldn't have any influence on the answer, note that we don't know in advance how many roots the function has...
I know I'm probably supposed to use the 'sign' function, because if i want an interval with just one root, i have to see if the y-values have a different sign

function [M] = intervals(a,b)
xt = test_fun([a:b]);
n = length(xt);
y = zeros(1,n);
for i = 1:n
y(i) = sign(xt(i));
for j = i+1:n
if (y(i) == y(j)); break; end
end
how do I continue?
I also started a while-loop once, "while the signs of the y values are the same, i don't have an interval yet...", but I got stuck there, too...

As an alternative i could use the plot of the function to 'see' how many intervals I'm supposed to look for, but that wouldn't give me a totally correct answer, but i'ts something...

This is due 5th of june, I hope someone can help me(Worried)...
• May 30th 2009, 02:17 AM
Quote:

y = x^3 - 9x + 0.1, that script should give me
M = [-2 1;1 4]
Either I am misunderstanding the question, or it shouldn't. There is no root between 1 and 4.

The sign function won't be able to help you very much, I'm afraid, since it is possible to have a root without a change of sign if the graph just touches the x-axis.

Are you sure you are not allowed to take advantage of the function? Cleaning up the constants produces $\displaystyle y = x_0\cos(pt)e^{qt}$. You probably know something about the roots of the cos and exponential functions that could allow you to calculate the roots from the values of p and q. I don't think that what the script is supposed to do is possible if it must work for every function.
• May 30th 2009, 10:42 PM
CaptainBlack
Quote:

hello,
I have to write a script that will give me a matrix M, with the intervals of x-values of a function, and every interval has JUST ONE root of the function:
every row of M has 2 elements (so M has 2 columns)
the elements are the first and last number of the interval, so the first column of M has the numbers of the second column of M, only one row lower.
The numbers has to be integers!!!:
for example:
y = x^3 - 9x + 0.1, that script should give me
M = [-2 1;1 4]
i have started a script like this:

xt = test_fun([a:b]) is a reference to another script I already made, that just gives me a vector xt with the y-values of a function
the function i have to work with
y = x0*cos((sqrt(4*m*k - b.^2))*t/(2*m)).*exp(-b*t/(2*m)),
but that shouldn't have any influence on the answer, note that we don't know in advance how many roots the function has...
I know I'm probably supposed to use the 'sign' function, because if i want an interval with just one root, i have to see if the y-values have a different sign

function [M] = intervals(a,b)
xt = test_fun([a:b]);
n = length(xt);
y = zeros(1,n);
for i = 1:n
y(i) = sign(xt(i));
for j = i+1:n
if (y(i) == y(j)); break; end
end
how do I continue?
I also started a while-loop once, "while the signs of the y values are the same, i don't have an interval yet...", but I got stuck there, too...

As an alternative i could use the plot of the function to 'see' how many intervals I'm supposed to look for, but that wouldn't give me a totally correct answer, but i'ts something...

This is due 5th of june, I hope someone can help me(Worried)...

Try something like:

Code:

 function [M] = intervals(a,b)   xx=a:b   xt = test_fun(xx);   n = length(xt);   ss=(xt>0);   ss1=ss(1:n-1);   ss2=ss(2:n);   dss=ss1-ss2;   idxs=find(dss);   sz=length(idxs);   M=zeros(sz,2);   for ii=1 to idxs     M(ii,:)=[xx(idxs(ii)),xx(idxs(ii)+1)];   end
Note all this does is find where your function changes sign on the grid defined by your arguments.

Also code not tested in this form, so you may need to debug it.

CB