Lab - for matlab PDF

Title Lab - for matlab
Course Fluid mechanics
Institution University of Nairobi
Pages 8
File Size 364 KB
File Type PDF
Total Downloads 44
Total Views 151

Summary

for matlab...


Description

Lab 1 - Your Name - MAT 275 Exercise 1.................................................................................................................................................1 Exercise 2.................................................................................................................................................1 Exercise 3.................................................................................................................................................4 Exercise 4.................................................................................................................................................4 Exercise 5.................................................................................................................................................5 Exercise 6.................................................................................................................................................6 The End!!!................................................................................................................................................7

Exercise 1 clc clear

% Define input variable theta as discretized row vector (i.e., array). theta = [0,pi/3,pi/2,2*pi/3,pi,4*pi/3];

% Define radius. r = 3;

% Define x and y in terms of theta and r. x = r*cos(theta); y = r*sin(theta);

% Check that x and y satisfy the equation of a circle.

r=sqrt(x.^2+y.^2)

r=

3.0000

3.0000

3.0000

3.0000

3.0000

3.0000

x and y satisfy the equation of a circle. This is because the both lie on the circle. The vector output at the end results to elements same as the radius thus confirming the answer.

Exercise 2 clear all % Define t-vector. t = linspace(1,10,46);

% Define y-vector. y=( exp(t./20).*cos(t))./(t.^2+4);

Part (a) % Plot results (should have 3 plots total). figure ; plot(y,'black','LineWidth',2) title('A plot for the function y= exp(t/20)cos(t)/(t^2+4)') % title

Part (b) % Plot results as data points only and as data points with line. figure ; % creates a new figure window for next plot plot(t,y,'blacko','LineWidth',2) title('A plot for the function y= exp(t/20)cos(t)/(t^2+4)') % title figure ; % creates another figure window plot(t,y,'blacko-','LineWidth',2) title('A plot for the function y= exp(t/20)cos(t)/(t^2+4)')

Exercise 3 clear all % Create t-vector (choose enough elements so that plot is smooth!) t = linspace(0,30,200); % Define x,y,z components in terms of t. x = sin(t); y = cos(t); z = t;

% Plot resuls. figure;

plot3(x,y,z) grid on

% plotting the circular helix % adding a grid to the plot

Exercise 4 clear % Define input variable as vector. x = linspace(-pi,pi,500); % Define y and z.

y = sin(x); z = x - (x.^3/6) + (x.^5/120);

% Plot results. figure; plot(x,y,'r',x,z,'b--') axis tight; grid on;

Exercise 5 clear all % invoke M-file ex5.m here type 'ex5.m'

% Runing/executing the M-file. run 'ex5.m'

% Exercise 5 function ex5

clear all

x = linspace(0,5,100); % define the vector x in the interval [0,5]

y1 = f(x,-2);

% compute the solution with C = -2

y2 = f(x,0);

% compute the solution with C = 0

y3 = f(x,2);

% compute the solution with C = 2

figure

% plot the three solutions with different line-styles and color plot(x,y1,'m-',x,y2,'b-*',x,y3,'go-')

title('Solutions to dy/dx = x^2-2x') % adding a title legend('C=(-2)','C=(0)','C=(2)','Location','Northwest'); % add a legend

end %--------------------------------------------------------------------------

function y = f(x,C)

y = (x.^3/3) - x.^2 + C; % fill-in with the expression for the general solution

end

Exercise 6 clear all % Part (a)

% Define f as anonymous function. f = @(x,y)(x^2+(x*exp(y))/(y+1));

% Evaluate f at x = -1 and y = 2

ans = -1.4630

Part (b) % Clear the function f out of the workspace. clear f % Print out f.m contents. type 'f.m'

% Evaluate f at x = -1, y = 2.

% Exercise 6 function f_out = f( x,y ) clear f

% clear the value of the function

f_out=(x^2+(x*exp(y))/(y+1)); % write a function M-file for the function

end ans = -1.4630

The End!!!...


Similar Free PDFs