├── readme └── Basic_of_Matlab.ipynb /readme: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Basic_of_Matlab.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "Basic of Matlab.ipynb", 7 | "provenance": [], 8 | "collapsed_sections": [], 9 | "toc_visible": true, 10 | "include_colab_link": true 11 | }, 12 | "kernelspec": { 13 | "name": "python3", 14 | "display_name": "Python 3" 15 | } 16 | }, 17 | "cells": [ 18 | { 19 | "cell_type": "markdown", 20 | "metadata": { 21 | "id": "view-in-github", 22 | "colab_type": "text" 23 | }, 24 | "source": [ 25 | "\"Open" 26 | ] 27 | }, 28 | { 29 | "cell_type": "code", 30 | "metadata": { 31 | "id": "_dca6992Zfrg", 32 | "colab_type": "code", 33 | "colab": {} 34 | }, 35 | "source": [ 36 | "!apt install octave" 37 | ], 38 | "execution_count": null, 39 | "outputs": [] 40 | }, 41 | { 42 | "cell_type": "code", 43 | "metadata": { 44 | "id": "poFRb32bZk1J", 45 | "colab_type": "code", 46 | "colab": {} 47 | }, 48 | "source": [ 49 | "%%writefile add.m\n", 50 | "x = 1\n", 51 | "y = 2\n", 52 | "display(x+y)" 53 | ], 54 | "execution_count": null, 55 | "outputs": [] 56 | }, 57 | { 58 | "cell_type": "code", 59 | "metadata": { 60 | "id": "7o_LGZZiZrDD", 61 | "colab_type": "code", 62 | "colab": {} 63 | }, 64 | "source": [ 65 | "# -W : no window system\n", 66 | "!octave -W add.m" 67 | ], 68 | "execution_count": null, 69 | "outputs": [] 70 | }, 71 | { 72 | "cell_type": "code", 73 | "metadata": { 74 | "id": "4cg5Y8WTZuNd", 75 | "colab_type": "code", 76 | "colab": {} 77 | }, 78 | "source": [ 79 | "x=1\n", 80 | "y=3\n", 81 | "display(x+y)" 82 | ], 83 | "execution_count": null, 84 | "outputs": [] 85 | }, 86 | { 87 | "cell_type": "markdown", 88 | "metadata": { 89 | "id": "dD8vVdlrlak-", 90 | "colab_type": "text" 91 | }, 92 | "source": [ 93 | "# **What is MATLAB**[4]\n", 94 | "MATLAB (matrix laboratory) is a fourth-generation high-level programming language and interactive environment for numerical computation, visualization and programming. MATLAB is a programming language developed by MathWorks.It can be run both under interactive sessions and as a batch job.It allows matrix manipulations; plotting of functions and data; implementation of algorithms; creation of user interfaces; interfacing with programs written in other languages, including C, C++, Java, and FORTRAN; analyze data; develop algorithms; and create models and applications.\n", 95 | "\n", 96 | "It has numerous built-in commands and math functions that help you in mathematical calculations, generating plots, and performing numerical methods.\n" 97 | ] 98 | }, 99 | { 100 | "cell_type": "markdown", 101 | "metadata": { 102 | "id": "aBxlAfKV8Fhx", 103 | "colab_type": "text" 104 | }, 105 | "source": [ 106 | "# **1-MATLAB environment**\n", 107 | "MATLAB environment consists of\n", 108 | "following main parts \n", 109 | "Command Window\n", 110 | "2. Command History\n", 111 | "3. Workspace\n", 112 | "4. Current Folder\n", 113 | "5. Editor Window" 114 | ] 115 | }, 116 | { 117 | "cell_type": "markdown", 118 | "metadata": { 119 | "id": "piepp1e28YCP", 120 | "colab_type": "text" 121 | }, 122 | "source": [ 123 | "![](https://drive.google.com/uc?export=view&id=1eAuQvW0sPUOeh_XRyyvEj4SoqcsS4Oma)" 124 | ] 125 | }, 126 | { 127 | "cell_type": "markdown", 128 | "metadata": { 129 | "id": "JH3K0V7h9AJG", 130 | "colab_type": "text" 131 | }, 132 | "source": [ 133 | "**Command Window**\n", 134 | "1. Main window in MATLAB.\n", 135 | "2. Used to enter variables.\n", 136 | "3. Used to run functions and\n", 137 | "M-file scripts.\n", 138 | "4. All commands are typed after\n", 139 | "command prompt “>>”.\n", 140 | "\n" 141 | ] 142 | }, 143 | { 144 | "cell_type": "markdown", 145 | "metadata": { 146 | "id": "3n3PmWPf9UjW", 147 | "colab_type": "text" 148 | }, 149 | "source": [ 150 | "![](https://drive.google.com/uc?export=view&id=1gJ9ZT-rbDon6f13_r1aLRE4LhofxQb73)" 151 | ] 152 | }, 153 | { 154 | "cell_type": "markdown", 155 | "metadata": { 156 | "id": "hkwIANA2-KaJ", 157 | "colab_type": "text" 158 | }, 159 | "source": [ 160 | "**Command History**\n", 161 | "1. Statements entered in\n", 162 | "command window are logged\n", 163 | "in Command History.\n", 164 | "2. View and search previously\n", 165 | "run statements.\n", 166 | "3. Copy and execute selected\n", 167 | "statements." 168 | ] 169 | }, 170 | { 171 | "cell_type": "markdown", 172 | "metadata": { 173 | "id": "f46UBVRF-txk", 174 | "colab_type": "text" 175 | }, 176 | "source": [ 177 | "![](https://drive.google.com/uc?export=view&id=18n-Afw5EsZhkDNIEC-V_Axy4TGLhpqCw\n", 178 | " )" 179 | ] 180 | }, 181 | { 182 | "cell_type": "markdown", 183 | "metadata": { 184 | "id": "9fvuj5jF_c8P", 185 | "colab_type": "text" 186 | }, 187 | "source": [ 188 | "**Workspace...**\n", 189 | "1. List all variables used as long\n", 190 | "as MATLAB has opened.\n", 191 | "2. Type “who” in command\n", 192 | "window to list all the\n", 193 | "commands used.\n", 194 | "3. Type “whos” to list all the\n", 195 | "commands with current\n", 196 | "values, dimensions, etc\n", 197 | "4. “clear” command is used to\n", 198 | "clear all the variables from\n", 199 | "workspace.\n", 200 | "5. Save all the variables and data\n", 201 | "to text file (.mat file) to use it\n", 202 | "for later." 203 | ] 204 | }, 205 | { 206 | "cell_type": "markdown", 207 | "metadata": { 208 | "id": "Eb1NrL0BAZSp", 209 | "colab_type": "text" 210 | }, 211 | "source": [ 212 | "![](https://drive.google.com/uc?export=view&id=1M0Y283M5lqTTjxBtd3PszyY9w6osNlub\n", 213 | " )" 214 | ] 215 | }, 216 | { 217 | "cell_type": "markdown", 218 | "metadata": { 219 | "id": "23UocFCiBHBC", 220 | "colab_type": "text" 221 | }, 222 | "source": [ 223 | "**Current Folder**\n", 224 | "1. Lists all m files, etc. available\n", 225 | "in current directory.\n", 226 | "2. Set working folder as current\n", 227 | "directory or as a part of the\n", 228 | "search path so that MATLAB\n", 229 | "will find the files easily" 230 | ] 231 | }, 232 | { 233 | "cell_type": "markdown", 234 | "metadata": { 235 | "id": "l1rJ_BEEBwAn", 236 | "colab_type": "text" 237 | }, 238 | "source": [ 239 | "![](https://drive.google.com/uc?export=view&id=17IWyoRq0bbxM-dzk2opykt8DztHADZu7)" 240 | ] 241 | }, 242 | { 243 | "cell_type": "markdown", 244 | "metadata": { 245 | "id": "48ZbE_tvG1yR", 246 | "colab_type": "text" 247 | }, 248 | "source": [ 249 | "**Editor**\n", 250 | "1. Used to create scripts and\n", 251 | "m-files.\n", 252 | "2. Click the “New Script” button\n", 253 | "in the Toolbar." 254 | ] 255 | }, 256 | { 257 | "cell_type": "markdown", 258 | "metadata": { 259 | "id": "rUKVOYgIHCtb", 260 | "colab_type": "text" 261 | }, 262 | "source": [ 263 | "![](https://drive.google.com/uc?export=view&id=1m9FUQ-F3WJhKlGVyx9ccafGhq5BfvNoq)" 264 | ] 265 | }, 266 | { 267 | "cell_type": "markdown", 268 | "metadata": { 269 | "id": "OgUqOhUjU5wa", 270 | "colab_type": "text" 271 | }, 272 | "source": [ 273 | "# **2- Variable**\n", 274 | "\n", 275 | "Variable is things that can varies.In MATLAB environment, every variable is an array or matrix.\n", 276 | "You can assign variables in a simple way. For example, \n", 277 | "- Defined with assignment operator, “=”.\n", 278 | "- Can be assigned without declaring their type and their type can\n", 279 | "change\n", 280 | "- Note that MATLAB is case sensitive! The variables x and X are not the\n", 281 | "same.\n", 282 | "- Once a variable is entered into the system, you can refer to it later.\n", 283 | "- Variables must have values before they are used.\n", 284 | "\n", 285 | "- When an expression returns a result that is not assigned to any variable, the system assigns it to a variable named ans, which can be used later.\n", 286 | "- To avoid choosing a name for a new variable that might conflict with a name already in use, check for any occurrences of the name using the which command\n", 287 | "\n", 288 | "\n" 289 | ] 290 | }, 291 | { 292 | "cell_type": "code", 293 | "metadata": { 294 | "id": "b3HoEhFLm3lC", 295 | "colab_type": "code", 296 | "colab": {} 297 | }, 298 | "source": [ 299 | "x = 3\t #defining x and initializing it with a value " 300 | ], 301 | "execution_count": 8, 302 | "outputs": [] 303 | }, 304 | { 305 | "cell_type": "markdown", 306 | "metadata": { 307 | "id": "U_wKbp7RnQU2", 308 | "colab_type": "text" 309 | }, 310 | "source": [ 311 | "It creates a 1-by-1 matrix named x and stores the value 3 in its element. Let us check another example," 312 | ] 313 | }, 314 | { 315 | "cell_type": "markdown", 316 | "metadata": { 317 | "id": "IJTIvn1knuqP", 318 | "colab_type": "text" 319 | }, 320 | "source": [ 321 | "defining x and initializing it with an expression" 322 | ] 323 | }, 324 | { 325 | "cell_type": "code", 326 | "metadata": { 327 | "id": "ToPqB_7YnYAe", 328 | "colab_type": "code", 329 | "colab": {} 330 | }, 331 | "source": [ 332 | "x = sqrt(16) \t" 333 | ], 334 | "execution_count": null, 335 | "outputs": [] 336 | }, 337 | { 338 | "cell_type": "code", 339 | "metadata": { 340 | "id": "YsgRJhoQoYOQ", 341 | "colab_type": "code", 342 | "colab": {} 343 | }, 344 | "source": [ 345 | "sqrt(78)" 346 | ], 347 | "execution_count": null, 348 | "outputs": [] 349 | }, 350 | { 351 | "cell_type": "code", 352 | "metadata": { 353 | "id": "mCHreQHAozIj", 354 | "colab_type": "code", 355 | "colab": {} 356 | }, 357 | "source": [ 358 | "x = 7 * 8;\n", 359 | "y = x * 7.89" 360 | ], 361 | "execution_count": 18, 362 | "outputs": [] 363 | }, 364 | { 365 | "cell_type": "markdown", 366 | "metadata": { 367 | "id": "HczOdWFdpCMv", 368 | "colab_type": "text" 369 | }, 370 | "source": [ 371 | "**Multiple Assignments**\n", 372 | "\n", 373 | "You can have multiple assignments on the same line. For example," 374 | ] 375 | }, 376 | { 377 | "cell_type": "code", 378 | "metadata": { 379 | "id": "kL-AvtLnpGQT", 380 | "colab_type": "code", 381 | "colab": {} 382 | }, 383 | "source": [ 384 | "a = 2; b = 7; c = a * b" 385 | ], 386 | "execution_count": 19, 387 | "outputs": [] 388 | }, 389 | { 390 | "cell_type": "markdown", 391 | "metadata": { 392 | "id": "28pSToYWpbET", 393 | "colab_type": "text" 394 | }, 395 | "source": [ 396 | "**I have forgotten the Variables!**\n", 397 | "\n", 398 | "The who command displays all the variable names you have used." 399 | ] 400 | }, 401 | { 402 | "cell_type": "code", 403 | "metadata": { 404 | "id": "RtMdP9ANpm_9", 405 | "colab_type": "code", 406 | "colab": {} 407 | }, 408 | "source": [ 409 | "who\n" 410 | ], 411 | "execution_count": null, 412 | "outputs": [] 413 | }, 414 | { 415 | "cell_type": "markdown", 416 | "metadata": { 417 | "id": "NakL8VAmpzvT", 418 | "colab_type": "text" 419 | }, 420 | "source": [ 421 | "MATLAB will execute the above statement and return the following result −\n", 422 | "\n", 423 | "Your variables are:\n", 424 | "\n", 425 | "a ans b c\n", 426 | "\n", 427 | "The whos command displays little more about the variables −\n", 428 | "\n", 429 | "- Variables currently in memory\n", 430 | "- Type of each variables\n", 431 | "- Memory allocated to each variable\n", 432 | "- Whether they are complex variables or not" 433 | ] 434 | }, 435 | { 436 | "cell_type": "markdown", 437 | "metadata": { 438 | "id": "tN0QB_X2qZvZ", 439 | "colab_type": "text" 440 | }, 441 | "source": [ 442 | "**Clear**\n", 443 | "\n", 444 | "The clear command deletes all (or the specified) variable(s) from the memory." 445 | ] 446 | }, 447 | { 448 | "cell_type": "code", 449 | "metadata": { 450 | "id": "ibycHx_6qf3S", 451 | "colab_type": "code", 452 | "colab": {} 453 | }, 454 | "source": [ 455 | "clear X # it will delete x, won't display anything\n", 456 | "clear # it will delete all variables in the workspace\n", 457 | " # peacefully and unobtrusively " 458 | ], 459 | "execution_count": null, 460 | "outputs": [] 461 | }, 462 | { 463 | "cell_type": "markdown", 464 | "metadata": { 465 | "id": "BgPD0ES2pbez", 466 | "colab_type": "text" 467 | }, 468 | "source": [ 469 | "**The format Command**\n", 470 | "\n", 471 | "By default, MATLAB displays numbers with four decimal place values. This is known as short format.\n", 472 | "However, if you want more precision, you need to use the format command.\n", 473 | "The format long command displays 16 digits after decimal.\n" 474 | ] 475 | }, 476 | { 477 | "cell_type": "code", 478 | "metadata": { 479 | "id": "64n4JEsgpxPB", 480 | "colab_type": "code", 481 | "colab": {} 482 | }, 483 | "source": [ 484 | "format long\n", 485 | "x = 7 + 10/3 + 5 ^ 1.2" 486 | ], 487 | "execution_count": null, 488 | "outputs": [] 489 | }, 490 | { 491 | "cell_type": "code", 492 | "metadata": { 493 | "id": "RatYZT0xqNJu", 494 | "colab_type": "code", 495 | "colab": {} 496 | }, 497 | "source": [ 498 | "format short\n", 499 | "x = 7 + 10/3 + 5 ^ 1.2" 500 | ], 501 | "execution_count": null, 502 | "outputs": [] 503 | }, 504 | { 505 | "cell_type": "markdown", 506 | "metadata": { 507 | "id": "RQAhXGI_qUWm", 508 | "colab_type": "text" 509 | }, 510 | "source": [ 511 | "MATLAB will execute the above statement and return the following result −\n", 512 | "\n", 513 | "x = 17.232" 514 | ] 515 | }, 516 | { 517 | "cell_type": "markdown", 518 | "metadata": { 519 | "id": "a0nTm2N0qfCf", 520 | "colab_type": "text" 521 | }, 522 | "source": [ 523 | "The format bank command rounds numbers to two decimal places. For example," 524 | ] 525 | }, 526 | { 527 | "cell_type": "code", 528 | "metadata": { 529 | "id": "Fc8moDfkqg3o", 530 | "colab_type": "code", 531 | "colab": {} 532 | }, 533 | "source": [ 534 | "Live Demo\n", 535 | "format bank\n", 536 | "daily_wage = 177.45;\n", 537 | "weekly_wage = daily_wage * 6" 538 | ], 539 | "execution_count": null, 540 | "outputs": [] 541 | }, 542 | { 543 | "cell_type": "markdown", 544 | "metadata": { 545 | "id": "aq-NIDLoqmgP", 546 | "colab_type": "text" 547 | }, 548 | "source": [ 549 | "MATLAB will execute the above statement and return the following result −\n", 550 | "\n" 551 | ] 552 | }, 553 | { 554 | "cell_type": "code", 555 | "metadata": { 556 | "id": "lDTXgO4zqojS", 557 | "colab_type": "code", 558 | "colab": {} 559 | }, 560 | "source": [ 561 | "weekly_wage = 1064.70" 562 | ], 563 | "execution_count": null, 564 | "outputs": [] 565 | }, 566 | { 567 | "cell_type": "markdown", 568 | "metadata": { 569 | "id": "qNqB3LGKrKB_", 570 | "colab_type": "text" 571 | }, 572 | "source": [ 573 | "#**3-Creating Vectors**\n", 574 | "A vector is a one-dimensional array of numbers. MATLAB allows creating two types of vectors −\n", 575 | "- Row vectors\n", 576 | "- Column vectors\n", 577 | "\n", 578 | "Row vectors are created by enclosing the set of elements in square brackets, using space or comma to delimit the elements." 579 | ] 580 | }, 581 | { 582 | "cell_type": "code", 583 | "metadata": { 584 | "id": "z2leJga5r_XG", 585 | "colab_type": "code", 586 | "colab": {} 587 | }, 588 | "source": [ 589 | "r = [7 8 9 10 11]" 590 | ], 591 | "execution_count": null, 592 | "outputs": [] 593 | }, 594 | { 595 | "cell_type": "code", 596 | "metadata": { 597 | "id": "6j--S-nRv7PX", 598 | "colab_type": "code", 599 | "colab": {} 600 | }, 601 | "source": [ 602 | "r = [7 8 9 10 11];\n", 603 | "t = [2, 3, 4, 5, 6];\n", 604 | "res = r + t" 605 | ], 606 | "execution_count": null, 607 | "outputs": [] 608 | }, 609 | { 610 | "cell_type": "code", 611 | "metadata": { 612 | "id": "22U3ErlowCBh", 613 | "colab_type": "code", 614 | "colab": {} 615 | }, 616 | "source": [ 617 | "res =\n", 618 | "\n", 619 | " 9 11 13 15 17" 620 | ], 621 | "execution_count": null, 622 | "outputs": [] 623 | }, 624 | { 625 | "cell_type": "markdown", 626 | "metadata": { 627 | "id": "2J5hjwhzwIdR", 628 | "colab_type": "text" 629 | }, 630 | "source": [ 631 | "Column vectors are created by enclosing the set of elements in square brackets, using semicolon(;) to delimit the elements." 632 | ] 633 | }, 634 | { 635 | "cell_type": "code", 636 | "metadata": { 637 | "id": "poHjihnawMGx", 638 | "colab_type": "code", 639 | "colab": {} 640 | }, 641 | "source": [ 642 | "c = [7; 8; 9; 10; 11]\n", 643 | "#c =\n", 644 | " 7 \n", 645 | " 8 \n", 646 | " 9 \n", 647 | " 10 \n", 648 | " 11 " 649 | ], 650 | "execution_count": null, 651 | "outputs": [] 652 | }, 653 | { 654 | "cell_type": "markdown", 655 | "metadata": { 656 | "id": "Kdq_N078waDr", 657 | "colab_type": "text" 658 | }, 659 | "source": [ 660 | "#**4-Creating Matrices**\n", 661 | "A matrix is a two-dimensional array of numbers.\n", 662 | "\n", 663 | "In MATLAB, a matrix is created by entering each row as a sequence of space or comma separated elements, and end of a row is demarcated by a semicolon. For example, let us create a 3-by-3 matrix as −" 664 | ] 665 | }, 666 | { 667 | "cell_type": "code", 668 | "metadata": { 669 | "id": "rXAR7UNtwxiJ", 670 | "colab_type": "code", 671 | "colab": {} 672 | }, 673 | "source": [ 674 | "m = [1 2 3; 4 5 6; 7 8 9]" 675 | ], 676 | "execution_count": null, 677 | "outputs": [] 678 | }, 679 | { 680 | "cell_type": "markdown", 681 | "metadata": { 682 | "id": "3hobbAdY3Tuq", 683 | "colab_type": "text" 684 | }, 685 | "source": [ 686 | "#**5-Commends**" 687 | ] 688 | }, 689 | { 690 | "cell_type": "markdown", 691 | "metadata": { 692 | "id": "6ytyTNmZ3mEY", 693 | "colab_type": "text" 694 | }, 695 | "source": [ 696 | "**Commands for Managing a Session**\n", 697 | "\n", 698 | "MATLAB provides various commands for managing a session. The following table provides all such commands −" 699 | ] 700 | }, 701 | { 702 | "cell_type": "markdown", 703 | "metadata": { 704 | "id": "C36JvehH4sTE", 705 | "colab_type": "text" 706 | }, 707 | "source": [ 708 | "- **clc**- Clears command window.\n", 709 | "- **clear**- Removes variables from memory.\n", 710 | "- **exist**- Checks for existence of file or variable.\n", 711 | "\n", 712 | "\t" 713 | ] 714 | }, 715 | { 716 | "cell_type": "markdown", 717 | "metadata": { 718 | "id": "cuk6Ah0vJZTn", 719 | "colab_type": "text" 720 | }, 721 | "source": [ 722 | "#**References**\n", 723 | "\n", 724 | "[1]How to run matlab .m files in google \n", 725 | "\n", 726 | "https://stackoverflow.com/questions/56416657/how-to-run-matlab-m-files-in-google-colab\n", 727 | "\n", 728 | "[2]MATLAB Software Installation r2020a\n", 729 | "https://www.youtube.com/watch?v=ZTefnp-miEI&feature=youtu.be&fbclid=IwAR2iMNmccJicoanxjzkCHFa0vWx7GnHMBRG6IltzG2HKuvonkZtCEG7WHuY\n", 730 | "\n", 731 | "[3]matlabcoding\n", 732 | "https://www.matlabcoding.com/\n", 733 | "\n", 734 | "[4]MATLAB Tutorial\n", 735 | "https://www.tutorialspoint.com/matlab/index.htm" 736 | ] 737 | } 738 | ] 739 | } --------------------------------------------------------------------------------