You are on page 1of 8

MATLAB: Assignment #1

Due on Monday, November 18, 2013

Dr Nasser Research Methods

Sam Hunter

Sam Hunter

MATLAB (Dr Nasser Research Methods): Assignment #1

Problem 1
An object thrown vertically with initial speed u reaches a height h at time t according to the equation h = ut gt2 2 (1)

Listing 1 shows a Matlab code for a function that takes the initial velocity and a time and calculates the height. Listing 1: Matlab function height.m
function [h] = height(u,t) g = 9.81; h = u*t - 0.5*g*(t).2; % calculate the height after given time and inital velocity

After 2 seconds an object thrown with initial velocity of 20 m/s will be 20.38 metres high.

Problem 2
Listing 2: Matlab script for nding when an object described by a parametric equation is closest to the origin
% Define the parametric equation t = 0:0.1:4; x = 5.*t -10; y = 25*t.2 -120*t +144; % Start by finding the distance at t=0 % Set it as the smallest distance so far smallestd = sqrt (x(1).2 + y(1).2); % For loop to calculate the distance at each point % and compare it with the closest so far and record it for i = 2: length (t) d = sqrt (x(i).2 + y(i).2); i f d < smallestd smallestd = d; smallestx = x(i); smallesty = y(i); T = 0.1*i; end end smallestd T % value smallestx smallesty % closest approach to the origin of t at closest approach % x value at closest approach % y value at closest approach

10

15

20

At t = 2.3 the object has coordinates (1,1) and has a distance of 1.4142 ( or

2) from the origin

Page 2 of 8

Sam Hunter

MATLAB (Dr Nasser Research Methods): Assignment #1

Problem 2

Problem 3
Plotting the equations y = 3x4 6x3 + 8x2 + 90 z = 3x3 + 5x2 8x over 3 x 3 Listing 3: Matlab script for plotting in 3D
% set up x = -3:0.1:3; y = 3*x.4 - 6*x.3 + 8*x.2 +90; z = 3*x.3 + 5*x.2 - 8*x; %plotting the functions h(:,1) = plot3 (x,y, zeros ( length (x)),b); hold on h(:,2) = plot3 (x, zeros ( length (x)),z,g); set (h(:,1),Color,b) % had issues here with legend colours being the same set (h(:,2),Color,g) % this workaround fixed it but i dont know why legend (h(1,:),y = 3x4 -6x3 +8x2+90, z = 3x3 + 5x2 - 8x) ylabel (y) xlabel (x) zlabel (z) grid on

(2) (3)

10

Figure 1: 3D Plot of y and z over x

Page 3 of 8

Sam Hunter

MATLAB (Dr Nasser Research Methods): Assignment #1

Problem 3

Problem 4
The depth of a well can be estimated using this equation d= g k t+ ekt 1 k (4)

Where d is the depth, g is gravitational acceleration 9.81 m/s2 , k is a friction coecient 0.05 and t is the time taken for the stone to hit the water. Listing 4: Takes an input of time and outputs how deep the well is
t = input (Time \n); k = 0.05; g = 9.81;
5

d = (g/k)*(t+((exp(-k*t)-1)/k)); disp ([The well is num2str(d) metres deep])

Problem 5
Given a number of points n the order of a polynomial derived from this set of points can be anything up to n 1. Generally the higher order polynomials are more accurate. The data being used is given below x y -1.00 -2.10 -0.70 0.30 -0.20 1.70 0.60 1.90 1.00 2.10

Using Nevilles algorithm I have written a script to t a polynomial of order n 1. Listing 5: Nevilles algorithm
% % x y
5

Langrangian interpolation of points with nevilles algorithm = [-1.00 -0.70 -0.20 0.60 1.00]; = [-2.10 0.30 1.70 1.90 2.10];

% Get number of x data points n = length (x); % the number of data points is directly related %to the order of the polynomial
10

15

% Form matrix A for X values X = ones(1,n);% for X^0 for i = 1:n X = [x.i; X]; % then every other power of X up to the amount of end X = X; %attempt to solve the system aX=Y % % a % solve linear system of equations using y as a column vector = (X\y) % a is a vector of coefficients of x with descending order of powers ie x^3 x^2 x^1 x^0

20

Problem 5 continued on next page. . .

Page 4 of 8

Sam Hunter

MATLAB (Dr Nasser Research Methods): Assignment #1 Problem 5 (continued)

The output is given as a vector of coecients for decreasing powers


a = 1.3017 -0.3895 0 -1.5324 0.7983 1.9220

comparing this with the matlab function polyt they are exactly the same.
p o l y f i t (x,y,5) ans =
5

1.3017

-0.3895

-1.5324

0.7983

1.9220

Problem 6
Given the following data from an excel spreadsheet. Import it and then compute the mean of the rows and then the sum of the rows 55 51 63 58 42 39 43 45 98 95 94 90

Listing 6: Excel reading script


data = xlsread(Book1.xlsx)% read in the spreadsheet rowavg = mean(data) % taking advantage of rowsum = sum(data)

the output is
rowavg = 65.0000
5

61.6667

66.6667

64.3333

rowsum = 195 185 200 193

Problem 7
Creating an array of equally spaced values from 0 to 2 . Then calculating two other arrays based on y = sin(4x) z = cos(4x) Plot them against each other and then separately. (5) (6)

Problem 7 continued on next page. . .

Page 5 of 8

Sam Hunter

MATLAB (Dr Nasser Research Methods): Assignment #1 Problem 7 (continued) Listing 7: 3D and subplotting

10

15

20

25

x = linspace (0, 2* pi ); %create vector of equally spaced values from 0 to 2pi y = sin (4*x); %set y and z z = cos (4*x); %plot on the same graph figure h(:,1) = plot3 (x,y, zeros ( length (x)),b); hold on h(:,2) = plot3 (x, zeros ( length (x)),z,g); set (h(:,1),Color,b) % had issues here with legend colours being the same set (h(:,2),Color,g) % this workaround fixed it but i dont know why legend (h(1,:),y = sin(4x), z = cos(4x)) ylabel (y) xlabel (x) zlabel (z) grid on % plotting on separate graphs in the same space figure subplot (2,1,1) plot (x,y,b) ylabel (y) xlabel (x) t i t l e (y = sin(4x)); grid on subplot (2,1,2) plot (x,z,g) ylabel (z); xlabel (x); t i t l e (z = cos(4x)); grid on

Plotting them on the same graph required using a 3D plot because they are functions in two dierent planes. This is shown in gure 2(a). This plot is good for comparing the two however its hard to see the dierences in the oscillations, gure 2(b) shows this by plotting the curves in their respective planes. Figure 2: Two ways of plotting on dierent axes

(a) Same Graph

(b) Separate Graphs

Page 6 of 8

Sam Hunter

MATLAB (Dr Nasser Research Methods): Assignment #1

Problem 7

Problem 8
Plot the surface and contours of the following function F (x, y ) = (5x2 + 2y 2 )e(x
2

+y 2 )

(7)

Listing 8: Plotting contours and surfaces


x = -2:0.1:2; % create x vector y = x; % use exactly the same for y [X,Y] = meshgrid(x,y); %create a meshgrid % calculate the value of F at every point on the grid F = (5*X.2+ 2*Y.2)*exp(-(X.2+Y.2)); figure %plot the surface surf (X,Y,F); xlabel (x) ylabel (y) zlabel (F) figure %plot the contours contour(X,Y,F,50); xlabel (x) ylabel (y)

10

15

Figure 3: Surface and contour plots

(a) Surface

(b) Contour

Page 7 of 8

Sam Hunter

MATLAB (Dr Nasser Research Methods): Assignment #1

Problem 8

Problem 9
Solving a standard quadratic of the form ax2 + bx + c. The code should be in the form of a function, take the three coecients and compute the roots of the equation then return them. Listing 9: Quadratic solver
%Function to solve a quadratic formula of the form ax^2 +bx +c %using the well known formula for finding the roots function [ roots ] = quadform(a,b,c) determinant = sqrt (b.2 - 4*a*c);
5

roots (1) = (-b - determinant)./a; roots (2) = (-b + determinant)./a;

Using this code, solve the following quadratics. 3x2 10x + 5 x2 + 4 x + 5


>> quadform(3,-10,5) ans =
5

(8) (9)

1.2251

5.4415

>> quadform(1,4,5) ans =


10

-4.0000 - 2.0000i

-4.0000 + 2.0000i

Problem 10
Write a conditional set of instructions to satisfy log(x) y= x x e 1 x > 10 0 x 10 x<0

(10)

Listing 10: Matlabe code for the piecewise function y


if x < 0 y = exp(x) -1; e l s e i f x <= 10 y = sqrt (x); else y = log (x); end

Page 8 of 8

You might also like