12.07.2015 Views

MatLab Tutorial

MatLab Tutorial

MatLab Tutorial

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

c. Numbered loops are useful when you have a specific number of repetitive commands for thecomputer to do. - Replace the previous m-file content with the following:close all;clear all;clc;sum_integers = 0;for i_loop = 1:100,sum_integers = sum_integers + i_loop;end;disp(['The sum of 1 through 100 is ',num2str(sum_integers)]);% 'num2str' converts a number to text (string) for printing.- Press F5.- Replace the previous m-file content with the following:% Graphing the sum of integers.close all;clear all;clc;size = 100;X = linspace(1, size, size); % Array of integers from 1 to 100.Results = zeros(1, size); % An array to store the answers.for iSumLoop = 1:size,Results(1, iSumLoop) = sum( X(1:iSumLoop) );end;disp('The sums in order are:');disp(' ');for iSumLoop = 1:size,disp([num2str(Results(1,iSumLoop))]);end;- Press F5.PRACTICE 3: Write a <strong>MatLab</strong> program that calculates the first 50 integers of the Fibonaccisequence. Save this program as 'Practice3.m' in your <strong>MatLab</strong> folder. Write down the 50th integerin the Fibonacci sequence here to check with your neighbors and mentor. Note: 1, 1, 2, 3, 5, …50th Fib = ________________________________

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!