{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Chapter 10: Curve Fitting Regression" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.1: Fitting_a_Straight_line.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 10_01\n", "//Fitting a Straight Line\n", "//Pg No. 326\n", "clear ;close ;clc ;\n", "\n", "x = poly(0,'x')\n", "X = 1:5\n", "Y = [ 3 4 5 6 8 ];\n", "n = length(X);\n", "b = ( n*sum(X.*Y) - sum(X)*sum(Y) )/( n*sum(X.*X) - (sum(X))^2 )\n", "a = sum(Y)/n - b*sum(X)/n\n", "disp(b,'b = ')\n", "disp(a,'a = ')\n", "y = a + b*x" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.2: Fitting_a_Power_Function_Model_to_given_data.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 10_02\n", "//Fitting a Power-Function model to given data\n", "//Pg No. 331\n", "clear ;close ;clc ;\n", "\n", "x = poly(0,'x');\n", "X = 1:5\n", "Y = [ 0.5 2 4.5 8 12.5 ]\n", "Xnew = log(X)\n", "Ynew = log(Y)\n", "n = length(Xnew)\n", "b = ( n*sum(Xnew.*Ynew) - sum(Xnew)*sum(Ynew) )/( n*sum(Xnew.*Xnew) - ( sum(Xnew) )^2 )\n", "lna = sum(Ynew)/n - b*sum(Xnew)/n\n", "a = exp(lna)\n", "disp(b,'b = ')\n", "disp(lna,'lna = ')\n", "disp(a,'a = ')\n", "printf('\n The power function equation obtained is \n y = %.4Gx^%.4G',a,b);" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.3: Fitting_a_Straight_line_using_Regression.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 10_03\n", "//Pg No. 332\n", "clear ;close ;clc ;\n", "\n", "time = 1:4\n", "T = [ 70 83 100 124 ]\n", "t = 6\n", "Fx = exp(time/4)\n", "n = length(Fx)\n", "Y = T ;\n", "b = ( n*sum(Fx.*Y) - sum(Fx)*sum(Y) )/( n*sum(Fx.*Fx) - (sum(Fx))^2 )\n", "a = sum(Y)/n - b*sum(Fx)/n\n", "disp(b,'b = ')\n", "disp(a,'a = ')\n", "printf('The relationship between T and t is \n T = %.4G*e^(t/4) + %.4G \n',b,a)\n", "deff('T = T(t)','T = b*exp(t/4) + a ')\n", "T_6 = T(6)\n", "\n", "disp(T_6,'The temperature at t = 6 is')" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.4: Curve_Fitting.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 10_04\n", "//Curve Fitting\n", "//Pg NO. 335\n", "clear ; close ; clc ;\n", "\n", "x = 1:4 ;\n", "y = [6 11 18 27 ];\n", "n = length(x) //Number of data points\n", "m = 2+1 //Number of unknowns\n", "disp('Using CA = B form , we get')\n", "for j = 1:m\n", " for k = 1:m\n", " C(j,k) = sum(x.^(j+k-2))\n", " end\n", " B(j) = sum( y.*( x.^( j-1 ) ) )\n", "end\n", "disp(B,'B = ',C,'C = ')\n", "A = inv(C)*B\n", "disp(A,'A = ')\n", "printf('Therefore the least sqaures polynomial is\n y = %i + %i*x + %i*x^2 \n',A(1),A(2),A(3))" ] } , { "cell_type": "markdown", "metadata": {}, "source": [ "## Example 10.5: Plane_Fitting.sce" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "//Example No. 10_05\n", "//Plane Fitting\n", "//Pg No. 342\n", "clear ; close ; clc ;\n", "\n", "x = 1:4\n", "z = 0:3\n", "y = 12:6:30\n", "n = length(x) //Number of data points\n", "m = 3 //Number of unknowns\n", "G = [ ones(1,n) ; x ; z]\n", "H = G'\n", "C = G*H\n", "B = y*H\n", "D = C\B'\n", "disp(C,B)\n", "disp(D)\n", "mprintf('\n The regression plane is \n y = %i + %f*x + %i*z ',D(1),D(2),D(3))" ] } ], "metadata": { "kernelspec": { "display_name": "Scilab", "language": "scilab", "name": "scilab" }, "language_info": { "file_extension": ".sce", "help_links": [ { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-octave", "name": "scilab", "version": "0.7.1" } }, "nbformat": 4, "nbformat_minor": 0 }