0% found this document useful (0 votes)
43 views

Project I Report Errors in Problem 1:: For If

The document summarizes errors found in three MATLAB code problems and proposed corrections. For Problem 1, it identifies two errors: 1) the sign in a conditional statement was incorrect, and 2) the index in a for loop did not match the array. For Problem 2, it identifies five errors: 1) the for loops did not properly iterate over the matrix dimensions, 2) an element was assigned incorrectly, 3) the wrong command was used to initialize an array, 4) an undefined variable was used, and 5) the function syntax was incorrect. For Problem 3, it identifies four errors: 1) a semicolon was missing, 2) the number of elements in an array was incorrect, 3) the wrong operator was used

Uploaded by

Julie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views

Project I Report Errors in Problem 1:: For If

The document summarizes errors found in three MATLAB code problems and proposed corrections. For Problem 1, it identifies two errors: 1) the sign in a conditional statement was incorrect, and 2) the index in a for loop did not match the array. For Problem 2, it identifies five errors: 1) the for loops did not properly iterate over the matrix dimensions, 2) an element was assigned incorrectly, 3) the wrong command was used to initialize an array, 4) an undefined variable was used, and 5) the function syntax was incorrect. For Problem 3, it identifies four errors: 1) a semicolon was missing, 2) the number of elements in an array was incorrect, 3) the wrong operator was used

Uploaded by

Julie
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Kruthi Annigeri

Nicholas Graham
CHE 205
T/Th 9:30-10:50
2 February 2018

PROJECT I REPORT

Errors in Problem 1:

1. In lines 8-9 of the original code, the following is written:

if temp_max_value > input_array(j)


temp_max_value = input array(i);

This suggests that if the temporary value, which is arbitrarily assigned to the first value of the array in
line 5, is greater than the array’s value at the index matching the for-loop’s iteration, then the
maximum value will change to that value. This doesn’t make sense when calculating a maximum, as the
temporary maximum value keeps getting replaced with smaller and smaller values. If the sign is flipped,
then the code will read the following, allowing a max-value to be found properly (save for one error, but
see #2):

if temp_max_value < input_array(j)


temp_max_value = input array(i);

2. Lines 7-8 of the original code say the following:

for i=2:length(input_array)
if temp_max_value > input_array(j)

Line 8 attempts to state a condition involving input_array’s value at index j, but the
for-loop is working in terms of index i. This mismatch makes line 8 meaningless in the for-loop, but
changing it to i makes the loop make sense. With the first two errors fixed, lines 7-9 should look like
this:

for i=2:length(input_array)
if temp_max_value < input_array(i)
temp_max_value = input_array(i);

3. The final change I made to the code could have been fixed either with me removing a semicolon from
the final line (line 13), or adding a line. I did the latter. Therefore, the original code from line 13 looked
like this:

max_value = temp_max_value;

I changed it so lines 13-14 now look like this:

max_value = temp_max_value;
disp(max_value);
Errors in Problem 2:

1. This problem uses nested for-loops, but the original code fails to use them effectively. In the original
code, lines 7-8 say the following:

for i = 1:m
for j = 1:m

Since the given size of A is [m, n], both m, the number of rows in A, and n, the number of columns in
A, should be used in the for-loop. Thus, I adjusted the code so that the value j tracks up to n instead m:

for i = 1:m
for j = 1:n

2. The second error occurs on the very next line, line 9. This program needs to invert or flip the matrix so
its dimensions are reversed, but this sets the new array temp so that it essentially equals A. Because temp
has dimensions (n, m), this omits some values in A. Thus, the original line 9 changes from the first
segment to the second:

temp(i, j) = A(i, j);

temp(j, i) = A(i, j);

3. The initialization of the temp array uses the incorrect command to create an array filled with the value
0. The command is zeros(n, m), but the original code reads zero(n, m).
4. Another such trivial error appears close to the last line, where the function sets output B equal to a
variable tmp. The variable tmp is never defined or used throughout the entire function, but we do want
to set B equal to temp. This may just be a typo or peccadillo, but it does cripple the code’s
functionality.
5. The final change I made to the code was in the first line of the code. Originally, the code read:

function B = Problem2_Project1_Spring_2018(A)

This is improper syntax to create a function with both inputs and outputs. Ideally, it would fit the form:

function [output] = fcn_name(input)

Thus, the corrected code reads:

function [B] = Problem2_Project1_Spring_2018(A)

Errors in Problem 3:

1. Line 3 of the original code reads:

E = 1e5; A = 7e16 R = 8.314;

The omission of the semicolon confuses the definition of A with the definition of R, so a semicolon is
needed after 7e16 for the three variables to be initialized correctly. The new corrected code reads:

E = 1e5; A = 7e16; R = 8.314;


2. The problem asks for a plot that displays reaction rate values in increments of 3 K. Between 253 and
325, there are 24 values at increments of 3 K. Therefore, another error occurred with regard to this in the
original code. On line 4, Ta is initialized as such:

Ta = linspace(253,325,500);

The third parameter being set to 500 creates a set of 500 values between 253 and 325, which means that
the increments are not 3 K but rather 0.144 K. Thus, I corrected the line to this:

Ta = linspace(253,325,24);

3. On line 6, the Arrhenius equation is programmed like so:

k = A * exp(–E / (R * Ta));

The lack of a dot before the slash, or division sign, signals a matrix operation, but because –E is not a
matrix, there is a mismatch. Rather, an element-by-element operation is needed. Thus, I corrected the
code to this:

k = A * exp(-E ./ (R * Ta));

4. Line 10 of the original code reads:

xlabel(Temperature, K)

The lack of single quotes around Temperature, K means that MATLAB does not interpret it as a
string. Therefore, I corrected the error so that line 10 now reads the following:

xlabel('Temperature, K')

Not only does the text turn purple to indicate that the code is now correct; the x-label displays properly,
as will be shown soon in the graph screenshot.

Part 2:

Unless writing a sub-function to compute factorials counts as a control structure, the only control structure I
used was the for-loop. All in all, I have four for-loops, and I will enumerate what each one does below:

1. The first two for-loops appear together, one of them being nested inside the other. The outer for-loop
tracks along every element of the array x_axis, and the inner for-loop is used for the Taylor Series
computation. Making the latter computation into a for-loop makes the actual code seamless and easy to
follow. Nesting it inside the for-loop that traverses the x_axis array (containing values from 0.1 to 3)
efficiently computes a sine approximation for all values from 0.1 to 3.
2. The third for-loop was used to calculate the percent error of the Taylor Series computation. It takes the
values put into the y_axis array from the first two for-loops and compares them to MATLAB’s
built-in sin(x) function. Every comparison, or percent error calculation, is put into a new
error_array, which will eventually be plotted against x_axis.
3. The fourth for-loop was for the factorial function that I created. The whole function is as follows:
function fctrl = factorial(m)
a = 1;
for n = 1:m
a = a*n;
end
fctrl = a;
end

The for-loop in this function is used to quickly go through all values from 1 to m and multiply them by
one another, as a factorial is the product of m and all positive integers below it. I was told later that
MATLAB has a factorial function already, but I was too satisfied with my insignificant function
that I’ve decided to keep it.

You might also like