L2. Basics Variables and Expressions Assignment Statements

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Lecture 4 Structure plan Program design process Program design process Basic elements of code: Basic elements of code: –Input (assignment of variables)
 2005 Pearson Education, Inc. All rights reserved Introduction.
1 Chapter 2 Introduction to Java Applications Introduction Java application programming Display ____________________ Obtain information from the.
Introduction to C++ September 12, Today’s Agenda Quick Review Check your programs from yesterday Another Simple Program: Adding Two Numbers Rules.
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Python. What is Python? A programming language we can use to communicate with the computer and solve problems We give the computer instructions that it.
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
Programming.
Lecture 4 Input/Output Conditional Statements 1. Data Types 2. Input/Outputs 3. Operators 4. Conditionals 1.
Quadratic Equations By Dr. Carol A. Marinas. Solving Equations In the previous section, we solved LINEAR equations. This means that the highest exponent.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
Copyright © The McGraw-Hill Companies, Inc. Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 1 An Overview of MATLAB.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
Fall, 2006Selection1 Choices, Choices, Choices! Selection in FORTRAN Nathan Friedman Fall, 2006.
Introduction to Programming with RAPTOR
Control Structures (A) Topics to cover here: Introduction to Control Structures in the algorithmic language Sequencing.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Asking the USER for values to use in a software 1 Input.
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Asking the USER for values to use in a software 1 Input.
WHAT IS A “SOLUTION”? Sect P.5 solving Equations.
Lecture 6: Output 1.Presenting results in a professional manner 2.semicolon, disp(), fprintf() 3.Placeholders 4.Special characters 5.Format-modifiers 1.
EGR 115 Introduction to Computing for Engineers Branching & Program Design – Part 4 Monday 06 Oct 2014 EGR 115 Introduction to Computing for Engineers.
GCSE Computing: Programming GCSE Programming Remembering Python.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
1 Agenda  Unit 7: Introduction to Programming Using JavaScript T. Jumana Abu Shmais – AOU - Riyadh.
Warm Up  1.) Write 15x 2 + 6x = 14x in standard form. (ax 2 + bx + c = 0)  2.) Evaluate b 2 – 4ac when a = 3, b = -6, and c = 5.
Solving Quadratic Equations by the Quadratic Formula.
Chapter 2 Writing Simple Programs
Bill Tucker Austin Community College COSC 1315
ECE 1304 Introduction to Electrical and Computer Engineering
Topics Designing a Program Input, Processing, and Output
Completing the Problem-Solving Process
Introduction to MATLAB for Engineers, Third Edition
Design & Technology Grade 7 Python
Variables, Expressions, and IO
Basic operations in Matlab
Introduction to Programming
Expressions and Interactivity
Section 11.2 The Quadratic Formula.
Programming Funamental slides
Language Basics.
Programming Funamental slides
T. Jumana Abu Shmais – AOU - Riyadh
Communication and Coding Theory Lab(CS491)
Quadratic Equations and Functions
Lecture 4 Structure plan
Introduction to Matlab
Class 2.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
JavaScript.
Topics Designing a Program Input, Processing, and Output
Engineering Problem Solving with C++ An Object Based Approach
Experiment No. (1) - an introduction to MATLAB
Topics Designing a Program Input, Processing, and Output
Using Script Files and Managing Data
Terminal-Based Programs
12th Computer Science – Unit 5
Variables in C Topics Naming Variables Declaring Variables
Programming Fundamental-1
Electrical and Computer Engineering Department SUNY – New Paltz
PYTHON - VARIABLES AND OPERATORS
Presentation transcript:

L2. Basics Variables and Expressions Assignment Statements Built-In Functions Scripts Comments Keyboard Input Formatting Output

Approach Preview key concepts by first playing with Matlab as a calculator. From formula to program.

Three Formulas Surface Area of a Sphere Half-Angle Quadratic Equation

Surface Area Increase >> r = 6365; >> delta = .000001; >> A_plus = 4*pi*(r+delta)^2; >> A = 4*pi*r^2; >> Increase = A_plus - A Increase = 0.15996992588043

Cosine(15 degrees) >> c = cos(pi/3); >> c = sqrt((1+c)/2); 0.96592582628907 >> c15 = cos(pi/12) c15 =

X2 + 5x + 6 = (x+2)(x+3) >> a = 1; >> b = 5; >> c = 6; >> d = sqrt(b^2 - 4*a*c); >> r1 = (-b - d)/(2*a) r1 = -3 >> r2 = (-b + d)/(2*a) r2 = -2

Let’s Revisit the Key Ideas Above and Introduce Others…

A Script % Quad1 % Solves x^2 + 5x + 6 = 0 a = 1; b = 5; c = 6; d = sqrt(b^2 -4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a)

Script A sequence of instructions. The order of the instructions is important. A script is a program.

Comments % Quad1 % Solves x^2 + 5x + 6 = 0 a = 1; b = 5; c = 6; d = sqrt(b^2 - 4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a)

Comments Begin with the “%” symbol. Goes to the end of the line. Facilitate the reading and understanding of the script.

Comments and Readability Start each program (script) with a concise description of what it does Define each important variable/constant Top a block of code for a specific task with a concise comment.

Arithmetic Expressions % Quad1 % Solves x^2 + 5x + 6 = 0 a = 1; b = 5; c = 6; d = sqrt(b^2 - 4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a)

Arithmetic Expression A recipe that results in the production of a number.

Built-In Functions % Quad1 % Solves x^2 + 5x + 6 = 0 a = 1; b = 5; d = sqrt(b^2 - 4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a)

Built-In Functions These are “packagings’’ of more advanced calculations. Some examples: log, exp, sin, cos,…

Variables % Quad1 % Solves x^2 + 5x + 6 = 0 a = 1; b = 5; c = 6; d = sqrt(b^2 - 4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a)

Variables A variable is a “box” that holds a numerical value. It has a name. The name must begin with a letter. Upper and lower cases are distinguished. Can use all letters and numbers and the underscore character. Example: x1A-New

Assignment Statements % Quad1 % Solves x^2 + 5x + 6 = 0 a = 1; b = 5; c = 6; d = sqrt(b^2 - 4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a)

Assignment Statements Variable Name Arithmetic Expression = where to put it a recipe for computing a numerical value

Script Execution a a = 1; b = 5; c = 6; b d = sqrt(b^2 -4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a) b c d r1 r2

Script Execution 1 a a = 1; b = 5; c = 6; b d = sqrt(b^2 -4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a) b c d r1 r2

Script Execution 1 a a = 1; b = 5; c = 6; 5 b d = sqrt(b^2 -4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a) 5 b c d r1 r2

Script Execution 1 a a = 1; b = 5; c = 6; 5 b d = sqrt(b^2 -4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a) 5 b c 6 d r1 r2

Script Execution 1 a a = 1; b = 5; c = 6; 5 b d = sqrt(b^2 -4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a) 5 b c 6 d 1 r1 r2

Script Execution 1 a a = 1; b = 5; c = 6; 5 b d = sqrt(b^2 -4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a) 5 b c 6 d 1 -3 r1 r2

Script Execution 1 a a = 1; b = 5; c = 6; 5 b d = sqrt(b^2 -4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a) 5 b c 6 d 1 -3 r1 -2 r2

Remember… Instructions are executed in order. In assignment statements, the right hand side is evaluated first and then the value is assigned to the variable named on the left hand side. The variables on the right hand side must have values before they can be used in an expression.

Question Time What is the value of X and Y after the following script is executed: X = 2; Y = 7*X; X = Y; X = X + 1; A. X is 5 and Y is 14 C. X is 5 and Y is 21 B. X is 15 and Y is 14 D. X is 15 and Y is 2

Question Time What is the final value of X and Y ? > X = 8; > X = Y; > X = 2*X; > Y = Y/2; A. X is 16 and Y is 16 C. X is 16 and Y is 4 B. X is 8 and Y is 8 D. X is 8 and Y is 4

Another Script % Quad2 % Solves ax^2 + bx + c = 0 % Assumes real roots. a = input('Enter a: '); b = input('Enter b: '); c = input('Enter c: '); d = sqrt(b^2 - 4*a*c); r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a)

The input Command Variable Name = input(‘ Message’); where to put it a prompt message in quotes Processed after the user hits the <enter> key.

Formatting Output When leaving off the semicolon isn’t good enough. The tools: disp, fprintf

disp Displays a string. Example: disp(‘This is a message’)

fprintf x = 1.23 Used to format output. Example: X = 1.23456789; fprintf(‘x = %5.2f\n’,x)) Output line will look like x = 1.23 The \n generates a carriage return

A Modification… r1 = (-b - d)/(2*a) r2 = (-b + d)/(2*a) disp(' ') fprintf('Root1 = %10.6f\n',r1)) fprintf('Root2 = %10.6f',r2))