i have a variable T=(2000:1:3000)......

%
I have a variable T=(2000:1:3000); and
y=exp(-22-10*T)./(5*T));
figure (1)
plot (y,T)
%but also have the polynomial
%4x^3+6x^2+(4+y)x-2y=0 ,which is translated to matlab
p=[(4 6 (4+y)-2y)];
so from the first part, for each T I get one y. But y is also a coefficient of the polynomial. That means I get 1000 polynomials. Now I want to get ONLY the positive roots from each polynomial in a matrix so a can create the plot(T,x)

Answers (1)

Why not just solve directly for x?
x^2 = y/(4+y)
so, for any y, you have x as
x = sqrt(y./(4+y));
As long as y is not exactly -4, this is valid. Can y EVERy be exactly -4? Of course not, since y is computed from an exponential.
A nice thing is, the above computation for x is fully vectorized, so it will work for all values of y, and therefore for all values of T.
As soon as you do this however, you will find that y is zero for ALL values of T however. Therefore, x will also be identically zero. That will be your next question, perhaps after you spend some minutes puzzling about what happened to your plot.
T is a number on the order of 2000 to 3000. Then you multiply by -10. So the product is on the order of -20000. Then you try to form the exponential, of a number on the order of -20000.
That result will underflow to zero. So, for ALL values ot T, the result y is identically zero, as computed in double precision. Plotting anything computed from it is silly anyway, since y will vary by thousands of powers of 10.
So I'm not at all sure what you expected to see by this exercise. The result will be rather boring when you do it.

2 Comments

my mistake. the above equations are just an example and you are right, the polynomial is simple. I just edit it to a much worst polynomial. The main point of this is how to subtract the positive roots of the polynomial and plot them...
Rant: :) Why do people always ask some trivially simple problem, but never tell us that their real problem is always more complex? Then we somehow needed to divine what the real problem was. End rant. :)
So you have something too difficult to do in one function call. So, WRITE A LOOP. Inside the loop...
Solve for the roots of each polynomial.
help roots
No, roots is not vectorized. It works on one polynomial at a time. Could you write code that would create arrays of polynomials, then pass them all into roots in some way? Well, yes, technically you can. If you don't see how to do this using a tool like cellfun or arrayfun, then it is not worth the mind sweat. The result would be unreadable to you and un-debuggable. And worst, it would not be any faster code than a simple loop here. Just use a loop. I suppose you could save all of the roots in one array. Then the remainder of the tests could be on the array of roots.
Decide which roots are acceptable to you. That will likely take the form of a test on the imaginary part of each root. If it is zero, then decide if the real part is positive.
help imag
help real
What will you do if more than one root is both real and positive? What if no roots are both real and positive? I can't answer those questions, only you can. Good code worries about such things. Bad code fails, then is the subject of a new frenzied post on Answers, about what to do when something unexpected comes up. So think about these problems in advance. Look for what can fail.
Save the roots that you are happy to see. Put them in a vector. Hopefully, this vector will be the same length as T. Then plot.
Break larger problems down into small problems. Solve each small problem.

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 30 Sep 2017

Edited:

on 1 Oct 2017

Community Treasure Hunt

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

Start Hunting!