Plot cubic roots in matlab - matlab

I would like to plot the roots of the cubic equation x^{3}+Ax^{2}+1=0 in matlab. I know that there are 3 real roots for A<-1.88 and 1 if A>-1.88. I would like to plot the 3 real roots as a function of A and when it switches to 1 real root and 2 complex to plot the real root and the real part of the complex conjugate solutions all in the same plot (perhaps as 2-3 graphs).
I am a matlab beginner though. I tried
syms x A
r = solve(x^3 + A*x^2+1 == 0, x);
ezplot(vpa(r(1)),[-10,10])
ezplot(vpa(r(2)),[-10,10])
ezplot(vpa(r(3)),[-10,10])
but vpa doesnt know how to numerically evaluate r.

There's no need to do symbolic math for this,
A = (-3:0.01:0)'; % create a vector of values for A
r = arrayfun(#(A)real(roots([1 A 0 1])),A,'uni',false); % calculate the polynomial roots for all values of A
r = [r{:}]; % convert result to a numeric array
plot(A,r'); % plot the result
grid on;
title('Real parts of Polynomial');
xlabel('A');

Related

How to create nonlinear power-law spaced vector in Matlab?

I'm trying to create a power law (x3) contour plot in the code.
Please look at the following question and the accepted answer: (logarithmic vector :temp)
How to create nonlinear spaced vector in Matlab?
I want a power law vector for temp instead of logarithmic.
Many thanks
In general, you can choose your spacing based on any function. For instance, here y is a function of x. But x is not linearly spaced. Instead, it is a function of another variable, t, which is linearly spaced. x(t) defines the spacing:
t = 0:.02:2;
x = #(u) u.^3; % or any other function
y = #(u) sin(u); % or any other function
figure; plot(x(t), y(x(t)), '*b')
With this output:

Solving Least square using MATLAB

Assume we want to determine the coefficients of a polynomial equation that is approximating the tangent function between 0 to 1, as follow:
-A is m×n vandermonde matrix. The entries are populated using m value between 0 to 11(given as input).
-The corresponding vector b is calculated using tangent function.
-x is calculated by typing x= A\b in MATLAB.
Now, using MATLAB, the computed x are subsittued in Ax. The result is plotted and it is pretty close to tangent function. But if I use polyval function of n−1 degree (in MATLAB) to calculate b, the resulting plot is significantly different from the original b. I cannot understand the reason for such a significant difference between the results of these two methods.
Here is the code:
clear all;
format long;
m = 60;
n = 11;
t = linspace(0,1,m);
A= fliplr(vander(t));
A=A(:,1:n);
b=tan(t');
x= A\b;
y=polyval(x, t);
plot(t,y,'r')
y2= A*x
hold on;
plot(t,y2,'g.');
hold on;
plot(t,tan(t),'--b');
Any insight would be appreciated. Thank you.
After A= fliplr(vander(t)) the A matrix is equal to
1 t(1) t(1)^2 ...
1 t(2) t(2)^2 ...
...
1 t(m) t(m)^2 ...
It is not correct because polyval accepts the coefficients in descending powers. You don't need to flip the columns of A:
A= vander(t);
A= A(:,end-n+1:end);

How can I use the Second Derivative test to find Maxima and Minima in MATLAB

I'm looking to create a function that takes an equation and marks the maxima and/or minima, and asymptotes on a graph.
From Calc 1, I remember using the Second Derivative Test.
I started by solving for the roots of the first derivative - but am not sure how I can plot where the point in this vector intersect my original equation.
syms x; %//
f = sin(x) %// Define equation as a function
df=diff(f) %// First derivatives
ddf=diff(df) %// Second Derivatives
I found the roots for the x-values of these points with dfRoots = solve(df)
and then created a vector of them called dfRoots_realDouble
dfRoots_double = double(dfRoots);
dfRoots_realDouble = real(dfRoots_double);
dfRoots_realDouble represent the x-values i need, but I dont know how to plot them as points and separate minima from maxima, etc
I'd like to be able to put in any function, like sin(6*(x^5) + 7*(x^3)+8*(x^2)) on [-1,1] and it highlight all the maxima with one marker, and all the minima with another.
If you sort the roots, you will have an alternating vector of maximums and minimums. Use the second derivative to determine if your vector of roots starts with a maximum or a minimum and then split the vector.
roots = sort([-3 4 2 -5]);
is_max = (subs(ddf, x, roots(1)) < 0)
if is_max
max_points = roots(1:2:end)
min_points = roots(2:2:end)
else
max_points = roots(2:2:end)
min_points = roots(1:2:end)
end
% plot with two different symbols
plot(max_points, subs(f, x, max_points), 'or',...
min_points, subs(f, x, min_points), '*k')

Insert x-value to polyfit vector to get y-value (Matlab)

I used the Matlab command polyfit to interpolate a curve. I then wanted to calculate the y-max value of that curve. I have found the roots for the polynomial, so i now have an x-value I want to insert in the polynomial from polyfit to get the y-value for that x-value.
I have difficulties to get it working properly.
P is the polynomial, so as you know, P(1)^4, P(2)^3... and so on
P = [-1.99405270507682e+26 5.55362828633395e+24 -5.80027044841956e+22 2.69238494640005e+20 -4.68659390860982e+17]
The x-value I want to insert to get a y-value is
x = 7.765633479578490e-04
The y-value should be about 17.7.
Am I thinking right here? The x-value is correct, I have compared it to my plot.
Thanks in advance guys!
Something is wrong in your calculations. With the values you give in your question, P(x) is enormous due to the enormous polynomial coefficients you have:
P =
-1.9941e+026 5.5536e+024 -5.8003e+022 2.6924e+020 -4.6866e+017
>> x = 7.765633479578490e-04
x = 7.7656e-004
>> y = polyval(P,x)
y = -2.9203e+017
These are the roots of your polynomial, they have very small imaginary parts, but given the enormous coefficients of the polynomial, they cannot be neglected:
>> format long g
>> roots(P)
ans =
0.00696463336211033 + 9.88579484856405e-07i
0.00696463336211033 - 9.88579484856405e-07i
0.00696084682252702 + 1.06977927834625e-06i
0.00696084682252702 - 1.06977927834625e-06i

Graphing polynom in Matlab

function x = plotpint (coef, x1, x2,numpoints)
%coef: polynom's coefficient array x1,x2 : intervals
%numpoints: the number of data points to be plotted
a=linspace(x1,x2,numpoints); % linspace of intervals and numpoints
b=polyval(coef,a); %values of given coef polynom for intervals
c=polyint(coef); % coef array of given coef's integral of polynom ( don't care here)
d=polyval(c,a); % values of c's polynom(integral of polynom) ( don't care here)
plot(a,b); % plotting polynom and integral of polynom
title('rer');
end
My plotpint function is here. For example I want to p(x) = x^2-4 which has roots -2 and +2. But when I tried to plot this polynom my graph shows that root is 0 like in image.
What should I do now? Where Am I wrong? How to fix this problem?
There is no problem, you've graphed y = x*x + 0*x + 4 correctly (according to your value of coef).
I do note that .0004 * 10^4 and .0000 * 10^4 are very hard to tell apart at that zoom level. You may see what's going on much more easily using the range -5:5 instead of -150:150.
FWIW, when interpreting any graph in MATLAB, start by looking at the axis scale.