Tuesday, September 15, 2015

Homework 5

clear;clc;
1.
%(a) Write a MATLAB® function called num_grains to find the number of
%grains in a 1-square-inch area (N ) at 100 x magnification when the ASTM
%grain size is known.
    %just did it. will call the function in next line
%(b) Use your function to find the number of grains for ASTM grain sizes
%n = 10 to 100
n=10:100;
num_grains(n);
%(c) Plot your results
    %done in function
    %%
    clear;
    clc;
function output= num_grains(n)
N=2.^(n-1);%equation that is used
output=N;%values that i get from the function
semilogy(n,N,'k-')%part c done in function to save time
title('number of grains in metal sample')
xlabel('n from 10 to 100')
ylabel('Number of grains in logrithmic form')


2. Create a function called polygon that draws a polygon with any number
%of sides. Your function should require a single input: the number of sides
% desired. It should not return any value to the command window but should
%draw the requested polygon in polar coordinates.
    %see function polygon, call with numberr of sides desired.
function output = polygon(s)
%this function answers question number two in the homework
%effectivly it creates a polygon based on user input

figure (2)
sides=s;%determines what polygon to create
degrees=2*pi/sides ;%draws polygon
theta=0:degrees:360-degrees ;% part one of polar graph
radius=ones(1,numel(theta)) ; % part 2 of polar grph
polar(theta,radius) % graphs polygon


%%
    clear;clc;


3. Perhaps the most famous equation in physics is

%E = mc2

%which relates energy E to mass m . The speed of light in a vacuum, c , is
%the property that links the two together. The speed of light in a vacuum is
%2.9979 * 108 m / s.

%(a) Create a function called energy to find the energy corresponding to a
%given mass in kilograms. Your result will be in joules, since
%1 kg m2 / s2 = 1 J.
    %see function energy
%(b) Use your function to find the energy corresponding to masses from 1 kg
%to 106 kg. Use the logspace function (consult help logspace ) to create an
%appropriate mass vector.
    m=logspace(0,6);%mass from 1 to 10^6kg
    E=energy(m); %energy of those masses
%(c) Create a plot of your results. Try using different logarithmic
%plotting approaches (e.g., semilogy , semilogx , and loglog ) to determine
%the best way to graph your results.
subplot(2,2,1);
semilogy(m,E)
xlabel('mass in kilograms')
ylabel('energy in Joules logged')
title('Energy Plot')
subplot(2,2,2)
semilogx(m,E)
xlabel('mass in kilograms logged')
ylabel('energy in Joules')
title('Energy Plot')
subplot(2,2,3)
loglog(m,E)
xlabel('mass in kilograms logged')
ylabel('energy in Joules logged')
title('Energy Plot')

function output=energy(m)
E=m.*(2.9979*10^8)^2%energy equation with c written as a number
output=E;



4.
%%
clear;clc;
%(a) Create a function M-fi le called distance to find the distance to the
%horizon. Your function should accept two vector inputs—radius and
%height—and should return the distance to the horizon. Don’t forget that
%you’ll need to use meshgrid because your inputs are vectors.

%(b) Create a MATLAB® program that uses your distance function to find the
%distance in miles to the horizon, both on the earth and on Mars, for hills
 %   from 0 to 10,000 feet. Remember to use consistent units in your
  %  calculations. Note that

%• Earth’s diameter = 7926 miles
REARTH=7929/2;RMARS=4217/2;
%• Mars’ diameter = 4217 miles

h=0:1000:10000;
height=h/5280;
From_Earth= distance(REARTH,height);
From_Mars=distance(RMARS,height);
height=height';
From_Earth=From_Earth';
From_Mars=From_Mars';

%Report your results in a table. Each column should represent a different
%planet, and each row a different hill height.
table(height,From_Earth,From_Mars)
function result=distance(r,h)

d=sqrt(2.*r.*h+h.^2);
result=d;function result=distance(r,h)


No comments:

Post a Comment