Tuesday, September 22, 2015

Day 7 - If Else, Switch Case

Day 7:

We learned about the find fucntions for matrices and other ways to call row and columns. We were also introudeced to & and | logic statements. We then moved on to if and else statements and then later onto switch and case structers.

We then did an example of following different and and or statemens on which ones returns 1's and 0's
Here is a picture.


Conclusion:
Today we learned about the find function which will be useful for targeting specific row and columns in data. We then got familiar with logic if else statements and also with switch case structures.


% HMWK 7
%7-1
t1= input('input number 1 \n')   %number1
t2 = input ('input number 2 \n')     %number2
t = input('input times \n')     %range number
x = (1:t);  %sets x range for counter
x(1) = t1;  %sets x1
x(2) = t2;  %sets x2
i = 3;  %counter start at 3

while i<=t;     %while loop i <t
    x(i)=x(i-2)+x(i-1);     %fib eq
    i=i+1;  %counter step 1
end

ang =(1:length(x));     %angle matrix of x
r = x;  %radius of x
polar(ang,r)    %graph
fprintf('the fibonacci seq to %d terms: %g \n',[t;x(t)]);   %display

%7-2
t1= input('input number 1 \n')   %number1
t2 = input ('input number 2 \n')     %number2
t= 1000;
%t = input('input times \n')     %range number
x = (1:t);  %sets x range for counter
x(1) = t1;  %sets x1
x(2) = t2;  %sets x2
i = 3;  %counter start at 3
while i<=t  %i counter to t
    x(i)=x(i-2)+x(i-1);     %feb counter
    i=i+1;  %step up of i
end
t=0;    %rest of t
k=3;    %set k counter to 3
while  (abs(x(k)/x(k-1) - x(k-1)/x(k-2))>0.001);    %while check for golden ratio
    t=t+1;  %t step up
    k=k+1;  %k step up
end
i = 3;  %set i 3
x=(1:t);    %set x matrix for counter

while i<=t  %i counter to t
    x(i)=x(i-2)+x(i-1); %feb counter
    i=i+1;  %i step up
end

ang =(1:length(x));     %angle matrix of x
r = x;  %radius of x
polar(ang,r)    %graph
fprintf('the number of fibonacci seq terms %d value number %g \n',[t;x(t)]);   %display

%%sqrt hmwk 7-2
function output = my_sqrt(num,e) %function for sqrt number guess
x = 1;  %intial value of x =1
y = (1/num)*(x^2);  %guess of Y
xg= (x/8)*(15-y*(10-3*y)); %number guess

while (abs((xg)-x)>e)   %loop for checking on e
    x = xg;     %update x to x guess
    y = (1/num)*(x^2);  %guess of Y
    xg = (x/8)*(15-y*(10-3*y)); %update x to x guess
end
output = xg;    %output

%%hmwk 7-2
n = input('input number of root\n '); %Number n input
ng = my_sqrt(n,0.001);  %number n guess by function
nr = sqrt(n);   %number calc of square root by matlab

fprintf('\nUsing Halley''s algoritm the square root of %g = %g. \n',[n;ng])
fprintf('The square root of %g calc by matlab is %g. \n', [n;nr]);

No comments:

Post a Comment