This preview shows pages 1–5. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Lecture 04 Todays class: Reminders about summation notation Summation using for and while loops for and while loops in Matlab Branching with if blocks in Matlab Absolute and relative errors [We will look in more detail at invoking functions using functions as arguments (function functions in Matlab ) later. Dont worry, I wont forget this!] Reminder 1 X k =1 a k = a 1 2 X =1 a = a 1 + a 2 3 X p =1 a p = a 1 + a 2 + a 3 4 X q =1 a q = a 1 + a 2 + a 3 + a 4 n X k =1 a k = a 1 + a 2 + a 3 + ... + a n = n X =1 a Summing with for loop Write a Matlab function sum_with_for that accepts a vector as input and returns the sum of its elements. Have your function use a for loop to compute the sum. function total = sum_with_for( a ) %SUM_WITH_FOR computes the sum of a vector's elements with for loop % Input: % a = vector of input numbers % Output: % total = computed sum total = 0; % Initialise running total n = length(a); % Determine # elements in sum for k=1:n % k plays role of summation index total = total + a(k); % Increment total end % end of for loop; k incremented end Summing with while loop Write a Matlab function sum_with_while that accepts a vector as input and returns the sum of its elements. Have your function use a...
View
Full
Document
This note was uploaded on 06/20/2011 for the course MATH 2070 taught by Professor Aruliahdhavidhe during the Winter '10 term at UOIT.
 Winter '10
 aruliahdhavidhe
 matlab

Click to edit the document details