MECH 3500: Computer Programming and Applications 8. plot(x, y,xx, yy,,y,\'o\',\'
ID: 3603133 • Letter: M
Question
MECH 3500: Computer Programming and Applications 8. plot(x, y,xx, yy,,y,'o','MarkerFaceColor','b','MarkerSize', 8); 9. axis(-44-4 41) 10. axis square 11. M(1) getframe; 12. for i-2:length(t) 13. x= s in (t(1)) ."(exp (cos (t(1)))-2-cos (4,*t (1))-sin(t(i)/12).^5); 14. y=cos ( t (1)).* (exp (cos ( t (1)))-2-cos (4, *t (1))-sin (t (1) /12)-5); 15. xx-1xx x: 16, yy= yy y]; 17. plot (x, y,xx,yy xy', 'MarkerFaceColor', 'b', 'MarkerSize',8) 18. axis(1-44-4 41) 19. axis square 20. M(i)-getframe; 21. endExplanation / Answer
A = (b+z*y)*y;
Area = inline('(b+z*y)*y','b','y','z')
» b = 2; z = 1.5; y = 0.75; A = Area(b,y,z)
Area = inline('(b+z.*y).*y','b','y','z') » b = [1, 2, 3]; z = [0.5, 1.0, 1.5]; y = [0.25, 0.50, 0.75];
» A = Area(b,y,z)
A =
0.2813 1.2500 3.0938
function [x,y,z] = CC(rho,theta,phi)
nr = length(rho);
nt = length(theta);
np = length(phi);
if nr ~= nt | nr ~= np | nt ~= np
error('Function CC - vectors rho, theta, phi have incompatible
dimensions')
abort
end
%Calculate coordinates if vector lengths are consistent
x=rho.*sin(phi).*cos(theta);
y=rho.*sin(phi).*sin(theta);
z=rho.*cos(phi);
» [x,y,z] = CC(2.5,pi/6,pi/8)
x =
0.8285
y =
0.4784
z =
2.3097
EDU» [x,y,z] = CC(rho,theta,phi)
x =
0.2985 0.2710 0.1568
y =
0.0800 0.1564 0.2716
z =
0.9511 1.9754 2.9836
function [y] = f(x)
y = zeros(3,1); %create output vector
y(1) = x(1)*cos(x(2))+x(2)*cos(x(1))+x(3);
y(2) = x(1)*x(2) + x(2)*x(3) + x(3)*x(1);
y(3) = x(1)^2 + 2*x(1)*x(2)*x(3) + x(3)^2;
» x = [1;2;3]
x =
1
2
3
» f(x)
ans =
function [J] = JJ(x)
J = zeros(2,2);
J(1,1) = x(1)+x(2);
J(1,2) = (x(1)+x(2))^2;
J(2,1) = x(1)*x(2);
J(2,2) = sqrt(x(1)+x(2));
function [] = FlowClassification(Re,Ma)
if Re <= 2000
class = 'The flow is laminar ';
elseif Re > 2000 & Re <= 5000
class = 'The flow is transitional ';
else
class = 'The flow is turbulent ';
end
%Classify according to Ma
if Ma < 1
class = strcat(class , ' and sub-sonic.');
elseif Ma == 1
class = strcat(class , ' and sonic.');
else
class = strcat(class , ' and super-sonic.');
end
%Print result of classification
disp(class)
function [] = FlowClassification()
%Request the input values of Re and Ma
Re = input('Enter value of the Reynolds number: ');
Ma = input('Enter value of the Mach number: ');
%Classify according to Re
if Re <= 2000
class = 'The flow is laminar ';
elseif Re > 2000 & Re <= 5000
class = 'The flow is transitional ';
else
class = 'The flow is turbulent ';
end
%Classify according to Ma
if Ma < 1
class = strcat(class , ' and sub-sonic.');
elseif Ma == 1
class = strcat(class , ' and sonic.');
else
class = strcat(class , ' and super-sonic.');
end
%Print result of classification
disp(class)
function [v] = MySort(u)
for i = 1:n-1
for j = i+1:n
if v(i)>v(j)
temp = v(i);
v(i) = v(j);
v(j) = temp;
end
end
end
function [v] = MySort(u,itype)
v = u;
%Start sorting process
if itype == 'i'
for i = 1:n-1
for j = i+1:n
if v(i)>v(j)
temp = v(i);
v(i) = v(j);
v(j) = temp;
end
end
end
else
for i = 1:n-1
for j = i+1:n
if v(i)<v(j)
temp = v(i);
v(i) = v(j);
v(j) = temp;
end
end
end
end
function [v] = swapVector(u)
n = length(u); %length of vector
v = u; %copy vector u to v
%Determine upper limit of swapping
if modulo(n,2) == 0 then
m = n/2;
else
m = (n-1)/2;
end
%Perform swapping
for j = 1:m
temp = v(j);
v(j) = v(n-j+1);
v(n-j+1) = temp;
end
function [] = Area()
%|------------------------------------|
disp('=============================')
disp('Open channel area calculation')
disp('=============================')
disp('Select an option:')
disp(' 1 - Trapezoidal')
disp(' 2 - Rectangular')
disp(' 3 - Triangular')
disp(' 4 - Circular')
disp('=============================')
itype = input('');
switch itype,
case 1
id = 'trapezoidal';
b = input('Enter bottom width:');
z = input('Enter side slope:');
y = input('Flow depth:');
A = (b+z*y)*y;
case 2
id = 'rectangular';
b = input('Enter bottom width:');
z = 0;
y = input('Flow depth:');
A = (b+z*y)*y;
case 3
id = 'triangular';
b = 0;
z = input('Enter side slope:');
y = input('Flow depth:');
A = (b+z*y)*y;
otherwise
id = 'circular';
D = input('Enter pipe diameter:');
y = input('Enter flow depth (y<D):');
beta = acos(1-2*y/D);
A = D^2/4*(beta+sin(beta)*cos(beta)) ;
end
%Print calculated area
fprintf(' The area for a %s cross-section is %10.6f. ',id,A)
function [] = myTable()
fprintf('====================================== ');
fprintf(' a b c d ')
fprintf('====================================== ');
for j = 1:10
a = sin(10*j);
b = a*cos(10*j);
c = a + b;
d = a - b;
fprintf('("%+6.5f %+6.5f %+6.5f %+6.5f ',a,b,c,d);
end
fprintf('====================================== ');
function [] = myTable()
fprintf('========================================================== ');
fprintf(' a b c d ')
fprintf('========================================================== ');
for j = 1:10
a = sin(10*j);
b = a*cos(10*j);
c = a + b;
d = a - b;
fprintf('%+6.5e %+6.5e %+6.5e %+6.5e ',a,b,c,d);
end
fprintf('========================================================== ');
function [S] = sum1(n)
S = 0; % Initialize sum to zero
k = 0; % Initialize index to zero
% loop for calculating summation
while k <= n
S = S + 1/(k^2+1); % add to summation
k = k + 1; % increase the index
end
function [S] = sum2(n)
[nr,nc] = size(n); %Size of input matrix
S = zeros(nr,nc); %Initialize sums to zero
%loop for calculating summation matrix
for i = 1:nr %sweep by rows
for j = 1:nc %sweep by columns
k = 0; %initialize index to zero
while k <= n(i,j)
S(i,j) = S(i,j) + 1/(k^2+1);
k = k + 1;
end
end
end
function [S] = sum2x2(n,m)
if length(n)>1 | length(m)>1 then
error('sum2 - n,m must be scalar values')
abort
end
%Calculate summation if n and m are scalars
S = 0; %initialize sum
for i = 1:n %sweep by index i
for j = 1:m %sweep by index j
S = S + 1/((i+j)^2+1);
end
end
function [z] = f2eval(x,y,f)
n = length(x);
m = length(y);
%Create a matrix nxm full of zeros
z = zeros(n,m);
%Fill the matrix with values of z
for i = 1:n
for j = 1:m
z(i,j) = f(x(i),y(j));
end
end
function [C] = matrixmult(A,B)
[nrA,ncA] = size(A);
[nrB,ncB] = size(B);
if ncA ~= nrB
error('matrixmult - incompatible matrices A and B');
abort;
end
%Calculate matrix product
C = zeros(nrA,ncB);
for i = 1:nrA
for j = 1:ncB
for k = 1:ncA
C(i,j) = C(i,j) + A(i,k)*B(k,j);
end
end
end
function [Table] = readtable()
fprintf('Reading a table from a file ');
fprintf('=========================== ');
fprintf(' ');
filname = input('Enter filename between quotes: ');
u = fopen(filname,'r'); %open input file
%The following 'while' loop reads data from the file
Table = []; %initialize empty matrix
while 1
line = fgetl(u); %read line
if ~ischar(line), break, end %stop when no more lines
available
Table = [Table; str2num(line)]; %convert to number and add to
matrix
end
%Determine size of input table (rows and columns)
[n m] = size(Table);
fprintf(' ');
fprintf('There are %d rows and %d columns in the table. ',n,m);
fclose(u);
function [] = myTableFile()
fprintf('Printing to a file ');
fprintf('================== ');
filname = input('Enter file to write to (between quotes): ');
u = fopen(filname,'w'); %open output file
fprintf('====================================== ');
fprintf(u,' ====================================== ');
fprintf(' a b c d ');
fprintf(u,' a b c d ');
fprintf('====================================== ');
fprintf(u,' ====================================== ');
for j = 1:10
a = sin(10*j);
b = a*cos(10*j);
c = a + b;
d = a - b;
fprintf('%+6.5f %+6.5f %+6.5f %+6.5f ',a,b,c,d);
fprintf(u,' %+6.5f %+6.5f %+6.5f %+6.5f ',a,b,c,d);
end;
fprintf('====================================== ');
fprintf(u,' ====================================== ');
fclose(u); %close output file
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.