{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Project Examples" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Average Function with Vectors" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are creating a function that takes 2 vectors, x and N, where x contains the values and N contains the number of times each value in x appears. We want to find the average of the values in x. " ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [], "source": [ "% this function is written and saved in a script file\n", "\n", "%function avg = myavg(x,N)\n", "\n", "%sizex = size(x); % recall these return 2 values, we want the 2nd value\n", "%sizeN = size(n);\n", "\n", "%if(sizex(2) ~= sizeN(2))\n", " %beep\n", " %disp('Error: number of elements does not match!')\n", "%else\n", " %total = sum(N); % total # of elements\n", " %s = x.*N; % element-by-element multiplication\n", " %avg = sum(s)/total;\n", "%end\n", "%end " ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " 55.8058\n" ] } ], "source": [ "x = [12 5 34 87 35 90 124];\n", "N = [2 4 56 12 6 9 14];\n", "myavg(x,N)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Function to Evaluate Polynomial for any Number" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% the following function is written and saved to a script file\n", "\n", "% function output = poly(x)\n", "\n", "% output = 1234*x.^3-43388*x.^2+531*x-123;\n", "\n", "% end" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " 24611322\n" ] } ], "source": [ "% now we can solve lengthy and complicated polynomials quickly\n", "poly(45)" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "ans =\n", "\n", " -41746 -162741\n", " -355704 -613231\n" ] } ], "source": [ "% this extends to using matrices as well\n", "poly([1 2; 3 4])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Automate Calculating Area of Triangle for any Base/Height " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This function will take user input to calculate the area. In order to accomplish this, we need to use the **input()** function." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% example usage of input()\n", "a = input('Enter the value for a: ')\n", "\n", "% Jupyter notebook doesnt allow for user input\n", "\n", "% the result in a MATLAB command window is:\n", "% Enter the value for a: 4\n", "\n", "% a =\n", "% 4\n", "\n", "% providing matrices will also work" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% we can also read strings \n", "Name = input('What is your name: ','s')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "% Here is the code for calculating area of triangle with user input \n", "b = input('Enter the value for the base: ');\n", "h = input('Enter the value for the height: ');\n", "area = 0.5 * b * h;\n", "disp(['The value for area is: ' num2str(area)])" ] } ], "metadata": { "kernelspec": { "display_name": "Matlab", "language": "matlab", "name": "matlab" }, "language_info": { "codemirror_mode": "octave", "file_extension": ".m", "help_links": [ { "text": "MetaKernel Magics", "url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md" } ], "mimetype": "text/x-matlab", "name": "matlab", "version": "0.8.0" } }, "nbformat": 4, "nbformat_minor": 0 }