MATLAB Programming Fundamentals - MathWorks

(やまだぃちぅ) #1

Calculate the Cube Roots


To execute individual sections of MATLAB code, go to the Live Editor tab and click the
Run Section button. Output appears together with the code that created it. Create
sections using the Section Break button.


In our case a, b, and c are all equal to 1. The other two roots are calculated from these
formulas:


a = 1 ; b = 1 ; c = 1;
roots = [];
roots(1) = 1;
roots(2) = (-b + sqrt(b^2 - 4ac))/(2a); % Use the quadratic formula
roots(3) = (-b - sqrt(b^2 - 4
ac))/(2a);


So the full set of cube roots of 1 are:


disp(roots')


1.0000 + 0.0000i
-0.5000 - 0.8660i
-0.5000 + 0.8660i


Displaying Roots in the Complex Plane


Include plots in the Live Editor so students can visualize important concepts.


We can visualize the roots in the complex plane to see their location.


range = 0:0.01:2*pi;
plot(cos(range),sin(range),'k') % Plot the unit circle
axis square; box off
ax = gca;
ax.XAxisLocation = 'origin';
ax.YAxisLocation = 'origin';
hold on
plot(real(roots), imag(roots), 'ro') % Plot the roots


Create Interactive Course Materials Using the Live Editor
Free download pdf