Example # 1
Resistors can be made out of many different materials and the value of their resistance is
expressed in Ohms (). The value of resistance can be expressed using this formula:
where P is resistivity (units Q-cm), L is resistor length (in cm) and A is cross-sectional area
For the following problem, assume all numbers have units as expressed above. You can also
assume that material resistivity is set to p- 10 Q-cm.
a) Write a function named resist that accepts the resistivity p. length L. and area A as input
arguments and returns as output arguments the value of resistance and error variable.
The function should work when p. L and A are scalar and when they are vectors.
The function itself should not prompt the user for input, and it should not print
The function should check that all input arguments are positive in which case
error variable should be set to logical false or o. If any of the input arguments are
negative or equal to zero the error variable should be set to logical true or 1.
Write a script that satisfies these criteria:
Prompt the user for the length L of one or more resistors. Store them in a vector.
Prompt the user for the cross-sectional area A of one or more resistors. Store them
Invoke the resist function and pass the p. L and A vectors to it.
If the value of the error variable is "true" display a message "Your data is
negative or equal to zero"
If the value of the error variable is "false" then print out the values of p. L. A and
the calculated resistance using a for loop (optional practice: use while loop
If you computer is Micosoft Windows based store your filc(s) in C: \temp-
Sample test data for verifying your results:
Suppose the user enters 10, 30, and 80 for the lengthsand 1, 2,and 2.5 for the areas-
Your output should look similar to the following table
Note You are not required to print out the text labels above cach column-
Enter length values [10 30 80]
Enter area values [1 2 2.5]
10.00 10.00 1.00 100.00
10.00 30.00 2.00 150.00
10.00 80.00 2.50 320.00
Various rates of discharge of a pump Werc measured and the corresponding
power required for cach discharge was recorded. as given below.
Table: Pump Power required to produce Discharge
a) Write a function slope that calculates the slope m of the least squares linear regression
function (see below).
It should have two input arguments: x and v. where x is independent variable and
yis dependent variable. Both x and y are vectors.
Use this equation to calculate the slope of the linear regression function
Summation is over the entire vector and n is the number of data points. Use for
loop(s) to accomplish summations. (optional practice: use while loop instead.)
b) Write a script that satisfies these criteria:
Set uptwo vectors: Q to store discharge values, and P to store power values, as given
in the table.
Prompt the user for the type of plot he/she would like. Options are: lin-lin, lin-log,
log-lin and og-log. Store user's choice in a variable.
Plot P vs. Q using the plot type selected by user. Properly label the axes and provide
Call function slope and pass 2and P as input arguments.
Use disp or fprintf to display the slope returned by function slope.
These solutions may offer step-by-step problem-solving explanations or good writing examples that include modern styles of formatting and construction
of bibliographies out of text citations and references. Students may use these solutions for personal skill-building and practice.
Unethical use is strictly forbidden.
function m = slope(X,Y)
sumX=sumX+X(i);%sum of x values
sumY=sumY+Y(i);%sum of y values
sumXsquare=sumXsquare+(X(i))^2;%sum of the square of each x value
sumYsquare=sumYsquare+(Y(i))^2;%sum of the square of each y value...