Question

In: Mechanical Engineering

This is a numerical methods question using MATLAB. Which of the following code snippets finds the...

This is a numerical methods question using MATLAB.

Which of the following code snippets finds the forward difference estimate of the derivative at each of the x values. Assume x and y have been previously defined, for example as

y=[10,20,25, 27.5, 30];
x = [0.3,0.5, 0.8, 0.9, 1];

(d is the derivative variable name)

Although not necessarily so, there may be more than one correct answer.

a) for k=1:length(y)-1
d(k)=(y(k+1)-y(k))/(x(k+1)-x(k));
end
d(k+1)=NaN
b) for k=1:length(y)
d(k)=(y(k+1)-y(k))/(x(k+1)-x(k));
end
c) d(1)=NaN;
for k=1:length(y)-1
d(k+1)=(y(k+1)-y(k))/(x(k+1)-x(k));
end
d) d(1)=NaN;
for k=2:length(y)
d(k)=(y(k)-y(k-1))/(x(k)-x(k-1));
end

Solutions

Expert Solution

x length is 5 and y length is also 5 there for obtained derivative array consist 4 value.

option a)

derivative array solved by using x and y arrays and stored in derivative array from 1 to 4 elements and non(NaN) value stored in derivative array as a 5th element.

option c)

derivative array solved by using x and y arrays and stored in derivative array from 2 to 5 elements and non(NaN) value stored in derivative array as a 1st element.

option d)

derivative array solved by using x and y arrays and stored in derivative array from 2 to 5 elements and non(NaN) value stored in derivative array as a 1st element.

finally

option b)

when we calculate the derivative array (for loop k=1 2 3 4 5)at 5th iteration means k=5

==>d(5)=(y(6)-y(5)/x(6)-x(5)) error occured due to there is no 6th element in x and y

option b is wrong answer remaing option are right

a and c,d are same but dealy is there that's why graph will shift right side one unit


Related Solutions

This is the Matlab practice so needs Matlab code 2. Numerical Integration Consider an industrial tank...
This is the Matlab practice so needs Matlab code 2. Numerical Integration Consider an industrial tank in the shape of an inverted cone. The radius of the tank at the top rim is 3 m, and the total height of the tank is 4 m. The volume of the tank in m3 is given by: V = (1/3) R2 H. The volume of liquid in the tank when filled to a height h measured from the bottom vertex is: V...
The following code must be written using matlab How to loop through a vector in matlab...
The following code must be written using matlab How to loop through a vector in matlab and assigning a value to every 4th entry. The vector could be of any length. Thanks
MATLAB code for the following question. In the game of Monopoly, a pair of dice are...
MATLAB code for the following question. In the game of Monopoly, a pair of dice are rolled to move a player's piece around the board. If a double is rolled (the dice show the same number), the player receives another roll of the dice. If a double is rolled a second time, a third roll of the dice is received. If a double is rolled on the third occasion, the player forfeits their turn (and goes to Jail). Write a...
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes...
Using Matlab: Using nested for loops (other methods are not allowed), write a function which takes as input a matrix, and as output returns true if at least one entry in the matrix is strictly greater than 8 and returns false otherwise.
For each of the following Visual Basic code snippets, identify the syntax error.   If intX >...
For each of the following Visual Basic code snippets, identify the syntax error.   If intX > 100   lblResult.Text = "Invalid Data" End If   Dim str As String = "Hello" Dim intLength As Integer intLength = Length(str) If intZ < 10 Then   lblResult.Text = "Invalid Data"   Dim str As String = "123" If str.IsNumeric Then   lblResult.Text = "It is a number." End If   Select Case intX   Case < 0     lblResult.Text = "Value too low."   Case > 100     lblResult.Text = "Value too high."   Case Else     lblResult.Text = "Value...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals for position 1, position 2, position3 and position 4 of the array. After these first 4 positions are drawn. The whole thing should start over where position5 drawn from same interval as positions 1, position6 drawn from same interval as...
The following code must be written using matlab and must be using a for-loop. NOTE! Write...
The following code must be written using matlab and must be using a for-loop. NOTE! Write a computer program that assigns random integers for each entry and generates a row vector. Different random integers should be drawn from different intervals in chunks of 4 , that is chunk1-chunk2-chunk3-chunk4 The parameters for specifying the lintervals by which the random numbers should be drawn should be able to change and be hardcoded in the script, however, be hardcoded in the script.
1. Calculate the pH of the following solution using numerical methods. a) 150 ml of 1.04...
1. Calculate the pH of the following solution using numerical methods. a) 150 ml of 1.04 × 10-6 M CH3COOH (pKA = 4.7) b) 150 ml of 1.04 × 10-6 M CH3COOH (pKA = 4.7) added with 500 ml of water c) 250 ml of 2.71 × 10-5 M HOCl (pKA = 7.6) d) 150 ml of 2.71 × 10-5 M HOCl (pKA = 7.6) added with 520 ml of water
Give me a MATLAB code which integrates any function using Romberg integration The code must work...
Give me a MATLAB code which integrates any function using Romberg integration The code must work fine or else I will down vote the answer badly Dont post if you dont know
Question 2. Explain preprocessor directives in C++. Describe Error and pragma directives and write code snippets....
Question 2. Explain preprocessor directives in C++. Describe Error and pragma directives and write code snippets. Describe define, and undef, directives. Write the code which shows if constant “VALUE” is not defined then define it and replace its value to 5 on runtime. Display the value of x. int x = VALUE;
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT