04 - Lecture 04 Todays class: Reminders about summation...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the 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.

Page1 / 11

04 - Lecture 04 Todays class: Reminders about summation...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online