Introductory Mathematica syntax

Slides:



Advertisements
Similar presentations
Switch code for Lab 4.2 switch (input) { /* input is a variable that we will test. */ case 'M': printf("The prefix is equal to 1E6.\n"); break; case 'k':
Advertisements

ZCE 111 Computational Approach in Physics Learning
Flow Charts, Loop Structures
Fall 2004ENGR 111A MatLab – Palm Chapter 4, Part 2 The if and switch structure Class 10.1 Sections: 4.4 and 4.6.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Introduction to Computing Dr. Nadeem A Khan. Lecture 4.
Review Algorithm Analysis Problem Solving Space Complexity
ZCE 111 Assignment 1. Q1, Q2: Loaning money from a long An a long is charging a interest rate of rate=2% per week. You borrow RM=1000 from this a long.
Loop Exercise 1 Suppose that you want to take out a loan for $10,000, with 18% APR, and you're willing to make payments of $1,200/month. How long will.
CS 127 Writing Simple Programs.  Stages involved  Analyze the problem  Understand as much as possible what is trying to be solved  Determine Specifications.
Chapter 12: How Long Can This Go On?
Problem Solving with the Sequential Logic Structure Lesson 5 McManusCOP10061.
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Loops (cont.). Loop Statements  while statement  do statement  for statement while ( condition ) statement; do { statement list; } while ( condition.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
Agenda Basic Logic Purpose if statement if / else statement
 2000 Deitel & Associates, Inc. All rights reserved. Chapter 10 - JavaScript/JScript: Control Structures II Outline 10.1Introduction 10.2Essentials of.
Ch13-1 Chap 13 Introduction to Matlab 13.1 Introduction MATLAB : The MATrix LABoratory program Not only is the MATLAB programming language exceptionally.
Chapter 7 Problem Solving with Loops
Structured Programming II: If Statements By the end of this class you should be able to: implement branching in a program describe and use an “if” statement.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 13 How Long Can This Go On?
COMP Loop Statements Yi Hong May 21, 2015.
Introduction to Literate Programming in Matlab 2WN50 – Week programming-in-matlab.pptx?dl=0.
BUSS 1 Financial planning: using break- even analysis to make decisions.
General Condition Loop A general condition loop just loops while some condition remains true. Note that the body of the loop should (eventually) change.
Control Structure  What is control Structure?  Types of Controls  Use the control structure in VBScript.  Example Summery.
Financial planning: break-even. Syllabus Candidates should be able to: define contribution and contribution per unit (selling price – variable cost per.
More about Iteration Victor Norman CS104. Reading Quiz.
DEVRY CIS 170 C I L AB 2 OF 7 D ECISIONS Check this A+ tutorial guideline at decisions For.
Repetition Statements
Discussion 4 eecs 183 Hannah Westra.
Chapter 9 Repetition.
Lesson Objectives Aims To be able to write an algorithm in Pseudo Code
Chapter 4 – C Program Control
Chapter 5: Structured Programming
MATLAB – More Script Files
REPETITION CONTROL STRUCTURE
Python: Experiencing IDLE, writing simple programs
Input and Output Upsorn Praphamontripong CS 1110
CHAPTER 6: REPETITION AND LOOP STATEMENTS
Control Flow Constructs: Conditional Logic
Repetition Structures Chapter 9
Chapter 2: Input, Processing, and Output
Python: Control Structures
Chapter 4 MATLAB Programming
Programming 101 Programming for non-programmers.
REPETITION STATEMENTS
Chapter Topics 2.1 Designing a Program 2.2 Output, Input, and Variables 2.3 Variable Assignment and Calculations 2.4 Variable Declarations and Data Types.
CSc 110, Autumn 2017 Lecture 5: The for Loop and user input
Topics Introduction to Repetition Structures
Problem Solving and Programming CS140: Introduction to Computing 1 8/21/13.
Chapter 5 Repetition.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Learning to Program in Python
Learning to Program in Python
Chapter 9 Control Structures.
Introduction to Object-Oriented Programming with Java--Wu
Algorithm Discovery and Design
Introduction to Algorithms and Programming
3. Decision Structures Rocky K. C. Chang 19 September 2018
Problem Solving Skill Area 305.1
Problem Solving.
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Python programming exercise
MatLab – Palm Chapter 4, Part 2 The if and switch structure
Chapter 2: Input, Processing, and Output
Problem 1 Given n, calculate 2n
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Presentation transcript:

Introductory Mathematica syntax Chapter 1 Introductory Mathematica syntax

Learn to use HELP and Documentation Center

Print a string Syntax: Print[] Every command line in Mathematica should by default end with a “;”, Print[“hello world”]; Assign a “value” to variable name: stringname= “hello world”; The “value” assigned to the variable stringname is not a numerical value, but a string. Syntax: StringQ[] Check that the variable stringname is assigned as a string using StringQ[stringname];

Short cut keys Execute a code: Shift + Enter Quit kernel: Alt + V  Q  Enter Clear All Output: Alt + C  L  Enter

Print a value Assign a numerical value of 1 and 2 to the variable named a and b: a=1;b=2; Define a new variable c as the sum of a and b: c=a+b; Print out the value of c: Print[c]; Syntax: NumberQ[] You can check whether the variable c is a numeric using NumberQ[c]

Do loop command Syntax: Do [ expression; ,{n,nbegin, nlast, interval} ]; Example: Print[“hello world for the ”,n, ”time”]; ,{n,1, 5, 1}

Print[“Hollo world”]; Semicolon “;” Each line of code in Mathematica must be ended with a semicolon “;” e.g, Print[“Hollo world”];

Use do loop to calculate the accumulative amount of money you owe to a long (charging you 2% per week) in 20 weeks. amount=5000;interestrate=0.02; Do[ interest=amount*interestrate; amount= amount+interest; Print[“This is the ”, n, “ week. The amount I owe a long is ”, interest]; ,{n,1,20,1} ];

Syntax: Input[], for interactive input amount = Input["Amount borrowed"]; interestrate = Input["interest rate, in percentage"]; nlast = Input["how many month to clear the loan"]; Print["amount=", amount]; Print["interestrate=", interestrate]; Print["nlast=", nlast];

Improved version: replace 20 by nlast=20; use Input[] for interactive input amount = Input["Amount borrowed"]; interestrate = Input["interest rate, in percentage"]; nlast = Input["how many month to clear the loan"]; Do[ interest=amount*interestrate; amount= amount+interest; Print[“This is the ”, n, “ week. The amount I owe a long is ”, interest]; ,{n,1,nlast,1} ];

If[] and Break[] command Syntax: If [condition, do something]; Syntax: Break [];

Exercise: Modify the previous code so that you can calculate the amount you still owe along in each week, and how many weeks it takes to clear off the loan if you are paying an installment of x per week. Incorporate the If and Break commands so that it can automatically break off the Do loop when the loan is cleared off. Assume: amount borrowed=5000;rate=2% per week;amout pay back per month=200; Sample code: http://comsics.usm.my/tlyoon/teaching/ZCE111_1516SEM2/notes/mathematicafiles/C1_along1.nb

Robustness Your code should be ‘robust’, i.e., (i) It should be able to automatically abort the programe if your installment is insufficient to pay back the loan ever (ii) The code must be able to provide the answer in which week the loan is cleared of (if ever), no matter how many weeks to takes, even if it is near infinity (this could occur if the installment is almost equal but just slightly larger than the first week interest). If your code is robust enough, it could be used in the future without making any modification except reassigning the values to these variables.

Flow Chart Input amount, rate,installment interest=amount*rate; No Yes Compare installment and first week interest. Possible to pay off loan ever? Input amount, rate,installment interest=amount*rate; Amout= amount – interest + installment No Yes Print message and abort Amount < 0 ? Yes Print message: n, amount; Stop No n = n +1

Syntax: While[] The previous along code is better implemented using the syntax While[ ];

While sample code Sample code: i = 0; While[i < 10, i = i + 1; Print[{"i=", i}] ]; amount = 5000; rate = 2; installment = 500; n = 0; While[amount > 0, n = n + 1; amount = amount +amount* (rate/100.0) - installment; Print[{n, amount}]; ];(*end while*)

Plotting amount owed vs. week The aloong while loop code has all the loan information for every week, e.g., amount owe from week 0 till the week you pay off the loan. So, can you plot the graph of amount owed to along as a function of number of week?

Syntax: List, Table, ListPlot Sample code: amount = 5000; rate = 0.02; installment = 500; n = 0; While[amount > 0, n = n + 1; amount = amount + (rate/100.0)* amount - installment; dummy[n] = amount; Print[{n, dummy[n]}]; ];(*end while *) ldummy = Table[dummy[i], {i, 1, n - 1}]; (* ldummy is a “List” *) lidummy = Table[{i, dummy[i]}, {i, 1, n - 1}] (* ldummy is a “List” *) ListPlot[lidummy]

Customising ListPlot Needs["PlotLegends`"]; lpt = ListPlot[lidummy, Joined -> True, Mesh -> All, PlotLabel -> "Amount owed vs. number of week", AxesLabel -> {"week", "amount owed"}, PlotMarkers -> "\[Times]", PlotStyle -> {Red, Dotted, Thin}, PlotLegend -> {"Amount owed"} ]; Print[lpt];

Summary In this Chapter, you have learned: Execute a code: Shift + Enter Quit kernel: Alt + V  Q  Enter Clear All Output: Alt + C  L  Enter Print[]; Do[]; NumberQ; StringQ; If[]; Break[]; Flowchart While[]; Assign a value to an element in a list: dummy[n] = value; List; Table; ListPlot; Customising a plot: Joined,Mesh -> All,PlotLabel, AxesLabel,PlotMarkers,PlotStyle,PlotLegend

Assignment With all these syntax, you must have already able to complete Assignment 1