Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

MSE 260

Homework 3

Spring ‘23

Answer all questions. Show all calculations, define all necessary terms, and show reasoning to receive credit. Absolutely no credit will be given for guessing! No handwritten/scanned work will be accepted. All work must be typed in whatever preferred format and all implemented MATLAB code must be included in your solutions. Submit your homework at http://submit.ncsu.edu

Problem 1: Loops and Conditional Statements

Try to work out what value ofx the following code returns (initially without running it). Draw a data flow chart (like the ones used in class) explaining the code.

x = 1;

if tan(73*pi*x/4) >= 0

x = 2;

else

x = pi;

end

if floor(x) ==x

x = 10;

else

x = 7;

end

if isprime(x)

x = ‘True’;

else

x = ‘False:

end

Is this result true whichever value ofx we start with? You can find out about the command isprime by typing help isprime.

Problem 2: Code correction for MATLAB.

a.         The following code is supposed to evaluate the function

0 x < 0

f (x) =

0 x > 2

x = linspace(-4,4);

N = length x

for j = 1;N

if x(j)>=0 and x(i)<=1

f(j) = x(j);

elseif x(j)>1 or x(i) <2

f(j) = 2 – x

else

f(j) = xero;

end

Correct the code so that it accompishes this. This can be checked by using the command plot(x,f) and comparing the figure to what you expect the function to look like.

b.        Explain, line-by-line the errors in and rewrite correctly the following code:

a = input(‘ Please entere a )

b = input(‘ Please enter b )

a = Input ‘ Please enter c

v1 = a+ B+d

v2 = a/((b+c)(c+a)

v3 = a/b*c

disp(‘ a + b + c= ‘ int2str(v1)])

disp([‘v2 = ‘ num2str v2 ]

disp([‘v3 = num2str(v4) ]);

Problem 3: Mannings equation, programming in MATLAB. (Chapra, Problem 3.9)

Manning’s equation can be used to compute the velocity of water in a rectangular open channel:

U = ( BH 2/3

where U = velocity (m/s), S = channel slope, n = roughness coefficient, B = width (m), and H = depth (m) . The following data are available for five channels:

n

S

B

H

0.036

0.0001

10

2

0.020

0.0002

8

1

0.015

0.0012

20

1.5

0.030

0.0007

25

3

0.022

0.0003

15

2.6

Write an M-file that computes the velocity for each of these channels. Enter these values into a matrix where each column represents a parameter and each row represents a channel. Have the M-file display the input data along with the computed velocity in tabular form where velocity is the fifth column.      Include headings on the table to label the columns.