Question

Problem 1 (20 pts) Global variables are declared outside any function and can be accessed by...

Problem 1 (20 pts)
Global variables are declared outside any function and can be accessed by any function in MATLAB. In contrast, local variables are declared inside a function, and can be used only inside that function. In this homework, you will create two global variables containing x and y coordinates of points to be plotted. You will also create a plotting function which will access the global variables.
a) Create the function plotFunct with no inputs and no outputs. Declare two global variables named x and y inside the function. This function will create a new figure and plot y vs x using green dots. Label the x-axis ’x data’ and y-axis ’y data’.
b) In a new script, declare two global variables x and y.
c) Initialize x to an array from 0 to 10 with a step size of 0.25.
d) Initialize y to be a function of x squared, then call the plotFunct to plot the global variables.
e) Repeat part d with y as the square root of x.
f) Repeat part d again with y as the natural log of x.
Note: Your code should produce three separate figures, one for each call made to plotFunct.
Please submit both .m files.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Note:

1. Two files have been created namely plotfunc.m and test.m

2. plotfunc.m has a function which is used to plot the graph and test.m is the script which contains the values of global variables x and y.

3. Run the test.m script to see the output plots.

4. Please set the octave or matlab working directly properly.

Code in plotfunc.m

function plotfunc()
   % x and y are global variables.
   global x;
   global y;
  
   %Using plot function to plot graph in dotted green.
   plot(x, y,'g:');
  
   %Labelling x and y axes.
   ylabel('y data');
   xlabel('x data');
end

Codes in test.m

1. For square function: (y = x2)

% x and y are global variables.
global x = []
global y = []

r = 1;

% Initializing each element of x.
for i=0:0.25:10
x(r) = i;
r = r+1;
end

% Initializing y as a square of x.
for i=1:length(x)
y(i) = x(i)^2;
end

plotfunc();

2. For square root function: y = sqrt(x) or y = x^0.5

% x and y are global variables.
global x = []
global y = []

r = 1;

% Initializing each element of x.
for i=0:0.25:10
x(r) = i;
r = r+1;
end

% Initializing y as a square root of x.
for i=1:length(x)
y(i) = x(i)^0.5;
end

plotfunc();

3. For natural log function: (y = ln x)

% x and y are global variables.
global x = []
global y = []

r = 1;

% Initializing each element of x.
for i=0:0.25:10
x(r) = i;
r = r+1;
end

% Initializing y as a square root of x.
for i=1:length(x)
%In matlab natural log is donated by log function.
y(i) = log(x(i));
end

plotfunc();

Output plots:

1.

2.

3.

Add a comment
Know the answer?
Add Answer to:
Problem 1 (20 pts) Global variables are declared outside any function and can be accessed by...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Examine the function below and answer the following questions. int gLobal = 25; // declared outside...

    Examine the function below and answer the following questions. int gLobal = 25; // declared outside function int func( int X, char *Y, BYTE Z ) { int a; char b[] = “The lazy dog ate the red fox before jumping over it.” char *c; c = (char *) malloc(512K); a = b[0]; return a; } -- b. How many bytes used on the stack for ‘Z’. c. How many 4K pages of physical memory are needed for the above...

  • . It is desired to write an interrupt driven software for the robot. Two global variables namely, currentStatus and previous Status should be declared that represent the current and previous stat...

    . It is desired to write an interrupt driven software for the robot. Two global variables namely, currentStatus and previous Status should be declared that represent the current and previous state of the robot, Initially, both should be set to STOPPED. The robot can have several states (for example, STOPPED, MOVING, COLLECTING, etc.). Write the following C function to initialize all /O ports (5 points) C function to initial the ADC (5 points) a. b. C. d. e. f. C...

  • Please help me with this MATLAB programming problem! Im coding iin MATLAB2018 if that makes any d...

    Please help me with this MATLAB programming problem! Im coding iin MATLAB2018 if that makes any difference! The first picture is the question i need to answer. The picture below is used as reference to answer the question. The last picture (below) is the into to the problem, and is used as reference. 1. Use Matlab to create the following single plot with three subplots. All titles, gridlines, and axis labels should be as shown. Arc System Response 15 E...

  • This uses MATLAB code with plotting HW19: Plotting with plot () Due 11:59 pm Monday 1....

    This uses MATLAB code with plotting HW19: Plotting with plot () Due 11:59 pm Monday 1. (10 pts) Write a script that will produce a sine curve and a cosine curve in the same plot, 0 as shown to the right. 06 0.4 0.2 To have two sets of data in one plot, you may plot the first one, then with the command hold on", plot the other. Or, you can learn how to plot two lines with just one...

  • MATLAB Problem HW7P2 (20 points) (5 pts) Write a user-defined MATLAB function called HW7P2_fn for the...

    MATLAB Problem HW7P2 (20 points) (5 pts) Write a user-defined MATLAB function called HW7P2_fn for the following math function 3 o-0.47x The input to the function is x and the output is y. Write the function such that x can be an array (use element-by-element operations) (15 pts) Use the function in (a) the command window to calculate y(-2) and y(5) (b) a script file HW7P2.m to determine y(x) for 0.001 Sx S 10 with 1000 points. Hint: Use the...

  • QUESTION 1 In Java, all variables declared using a class are ____. a. reference variables b....

    QUESTION 1 In Java, all variables declared using a class are ____. a. reference variables b. constants c. primitive variables d. operators 1 points    QUESTION 2 An expression such as str.length(); is an example of a(n) ____. a. system call b. method call c. object call d. class 1 points    QUESTION 3 Consider the following statements. double x; String y; y = String.format( "%.2f", x); If x = 285.679, what is the value of y? a. "285.00" b....

  • Create a #define called MAPSIZE. Set to 10. Create global variables DirectionsFilename and MapFilename. Use char...

    Create a #define called MAPSIZE. Set to 10. Create global variables DirectionsFilename and MapFilename. Use char arrays and set them to nulls by using {}. Create a structure called RowCol that has two int members called row and col to hold the row and column. Function MoveNorth should have a return value of void and a parameter of a pointer to RowCol. { To move north, you need to subtract 1 from PositionPtr->row. Before doing so, you need to check...

  • Problem 1 (10 pts) (Matlab coding) In this problem you will be manipulating a sine wave...

    Problem 1 (10 pts) (Matlab coding) In this problem you will be manipulating a sine wave plot using a for loop and if statements. a) Create a vector x that goes from −4π to 4π with increments of π/10. b) Instead of using y = sin(x) on the entire array at once, use a for loop to calculate the sine of x for each value in the vector individually. Vector y should be the sine of vector x. Use the...

  • A group of physics students collected data from a test of the projectile motion problem that...

    A group of physics students collected data from a test of the projectile motion problem that was analyzed in a previous lab exercise (L5). In their test, the students varied the angle and initial velocity Vo at which the projectile was launched, and then measured the resulting time of flight (tright). Note that tright was the dependent variable, while and Vo were independent variables. The results are listed below. (degrees) Time of Flight (s) Initial Velocity V. (m/s) 15 20...

  • IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. •...

    IN C++ Please!! Declare a global integer constant called SIZE and initialize it to 10. • Declare a global enum variable that will support 10 values – each representing an ant colony {A, B, C, D, E, F, G, H, I, J}. • In the main function, o Declare a 2-dimensional array of size the global integer constant, SIZE. The number of rows and columns should be equal to SIZE, which would make this a square matrix. This array will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT