Problem 1 - Write a Matlab script that evaluates the following piecewise function for any user inputs x1 and x2.
Test you code using the following values: f(-2, 3) = 7, f (2, -3) = 10, f(2, 3) = 11, f (-2, -3) = 13.
Problem 2 - Prepare a Matlab code to calculates the function
y(x) = In 1/(x2-25)
for any legal user-input x. If the inserted value of x is illegal, report that to the user by printing an error message on the Matlab command window. Note that in the above equation, In is the natural algorithm to the base e, the Matlab function for this mathematical function is log.
Problem 3 - Write a script file that creates a plot of the function shown below. Plot the function from x = -2 to x = 4 (use an increment of 0.01 for your x-vector). Label the axes as "X-Axis" and Y-Axis" respectively, and turn the grid on.
Hint: Be sure to use your "." operator!
f(x) = 3.5-0.5x cos(6x)
Problem 4 - An experiment is performed to measure the intensity of a source of light over a distance. The experiment yielded the following data:
Distance (cm)
|
10
|
12
|
14
|
16
|
18
|
20
|
22
|
"x" values
|
Intensity (lux)
|
950
|
640
|
460
|
340
|
250
|
180
|
140
|
"y" values
|
Write a script file that plots both the experimental data above and the theoretical equation shown below.
y = 95000/x2
For the variables:
- Create a vector containing experimental distance [Use xe as the variable name].
- Create a vector containing experimental intensity [Use ye as the variable name).
- Create a vector containing theoretical distance [Use xt as the variable name) - This vector should contain values from x = 10 to x = 22 at an increment of 0.05.
- Create a vector containing theoretical intensity [Use yt as the variable name). Be sure to use your "." operator.
For the plot:
Plot the experimental data first, using a red dashed line with circle markers. Use a line width of 1.0 pixels and a marker size of 8.0 pixels.
Title the chart as "Light Intensity as a Function of Distance." Label the x-axis "Distance (cm)" and the y-axis "Intensity (lux)."
Plot such that the x-axis is bounded from 8 to 24 and the y-axis is bounded from 0 to 1200. Also, turn the grid on.
Plot the theoretical model data next, using a line width of 1.0 pixels.
Last, create a legend, labeling the data sets "Experimental" and "Theoretical," respectively.
Problem 5 - For the experimental data points below, write a script that will fit two polynomials (a line and a parabola) to see which predicts the data best and p of the results.
X-Values
|
0.0
|
0.5
|
1.0
|
1.5
|
2.0
|
2.5
|
3.0
|
3.5
|
4.0
|
4.5
|
5.0
|
Y-Values
|
6.00
|
4.83
|
3.70
|
3.15
|
2.41
|
1.83
|
1.49
|
1.21
|
0.96
|
0.73
|
0.64
|
For the variables:
- Create a vector containing experimental X-Values [Use x as the variable name]
- Create a vector containing experimental Y-Values [Use y as the variable name]
- Use polyfit( ) to fit a line to the data [Use p1 as the variable name]
- Use polyval( ) to compute corresponding y-values for x [Use y1 as the variable name]
- Use polyfit( ) to fit a parabola to the data [Use p2 as the variable name]
- Use polyval( ) to compute corresponding y-values for x (Use y2 as the variable name]
For the plot:
- Plot the experimental data first, using circle markers only and no other formatting. To do this, just specify ' o
- Label the axes as "X-Axis" and Y-Axis" respectively, and turn the grid on."
- Note that you don't need to use the hold command. Instead, just plot all of the data sets with a single plot ( ) command.
Finally, using the more accurate polynomial, use polyval ( ) to determine the predicted value at x = 4.25. Assign this to variable c at the end of your script.
Please do it and explain how you did it?