{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "The roots of quadratic equations $a x^2 + b x + c = 0$ are $ r = \\frac{-b \\pm \\sqrt{b^2 - 4ac}}{2a}$." ] }, { "cell_type": "code", "execution_count": 21, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "roots are positive.\r\n" ] } ], "source": [ "a=1; b=0; c=-3;\n", "d=b^2-4*a*c;\n", "if d>=0\n", " disp('roots are positive.');\n", "end" ] }, { "cell_type": "code", "execution_count": 22, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "roots are immaginary\r\n" ] } ], "source": [ "a=1; b=1; c=3;\n", "d=b^2-4*a*c;\n", "if d>=0\n", " disp('roots are positive.');\n", "else\n", " disp('roots are immaginary');\n", "end" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "roots are immaginary\n", "r1 = -0.500000 + i 1.658312 \n", "r2 = -0.500000 - i 1.658312 \n" ] } ], "source": [ "a=1; b=1; c=3;\n", "d=b^2-4*a*c;\n", "if d>0\n", " disp('roots are positive and distinct');\n", " r1 = (-b+sqrt(d))/(2*a)\n", " r2 = (-b-sqrt(d))/(2*a)\n", "elseif d==0\n", " disp('roots are real and same');\n", " r = -b/(2*a);\n", "else\n", " disp('roots are immaginary');\n", " re = -b/(2*a); im = sqrt(-d)/(2*a);\n", " printf (\"r1 = %f + i %f \\n\",re, im);\n", " printf (\"r2 = %f - i %f \\n\",re, im);\n", "end" ] }, { "cell_type": "code", "execution_count": 24, "metadata": { "collapsed": true }, "outputs": [], "source": [ "%plot gnuplot" ] }, { "cell_type": "code", "execution_count": 25, "metadata": { "collapsed": false }, "outputs": [], "source": [ "% define a quadretic function with a, b and c parameters\n", "f = @(x, a, b, c) a*x.^2 + b*x + c;\n", "\n", "x= -3:0.1:3;\n", "y1=f(x,1,1,-1); %we get two real roots \n", "y2=f(x,1,-2,1); %one real root\n", "y3=f(x,1,1,1); %no real root\n", "\n", "plot(x,y1,'r',x,y2,'g',x,y3,'b')\n", "hold on\n", "plot(x,0*x,'k',0*ones(length(x),1),10*x,'k') %plotting axises\n", "hold off\n", "axis([-3, 3, -5,20])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "\"Quadretic" ] } ], "metadata": { "kernelspec": { "display_name": "Octave", "language": "octave", "name": "octave" }, "language_info": { "file_extension": ".m", "help_links": [ { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-octave", "name": "octave", "version": "0.16.1" } }, "nbformat": 4, "nbformat_minor": 1 }