ENGR 215 Fall 2016 Programming with MATLAB Programming Assig

ENGR 215 Fall 2016 Programming with MATLAB Programming Assignment 4 (Due Date: Oct. 23, 2016) (For this assignment, you will need to email a MATLAB script called \"falll 6assn4.m\". This script must be well- commented so that your work can be followed.) Data has been collected (for a 30 year period-1975 to 2004) measuring sunspots. These spots are relatively \"cool\" regions on the sun that appear as dark spots when observed through special solar filters. The number of sunspots varies in an 11-year cycle. You will be given data in a data file that represents the year and the number of sunspots per year. You will plot the original data as a scatter plot and use the cosine function to model the data. Using the following steps, construct the cosine model that will best \"fit this data. Each one of the plots that you graph should be on the same fizure so you can see each movement. Your first plot will be just points (no line in black and a solid ling. Add a legend to your figure (containing all the plots) to help you see each graph 1. Load the data from the MATLAB data file: prog4data.mat. The data file already has the two variables (mamuss and year) and the data for the corresponding years and number of sunspots per year over the 30 year period. Plot the data as points only (do not connect the points with any lines), use magenta as the color and add a grid on your graph. Label your horizontal and vertical axis as YEAR\' and respectively \'NUMSS and assign a title to the graph. The data should represent the number of sunspots versus the year 2. Notice that the data looks like a \"cosine\" wave that has an amplitude greater than 1, a vertical shift, a horizontal (phase) shift and a change in frequency. Create y, a \"normal\" cosine wave, (with an amplitude of I) using the x data values and plot this cosine curve on the same graph as the scatter plot, using cyan for the color. Your x values should run from the first year value to the last year value and have a short increment (an increment of 0.25 will suffice) to make a \"smooth\" curve. You may need to experiment with the values to get a good plot. 3. To adjust the cosine vertically, we need to move the midpoint of the cosine wave (located at 0) to the midpoint of the data. To do this, you will need to calculate the midpoint of the data by finding vs, which is the average (mean) of the maximum value of the data and the minimum value of the data (these max and min values correspond to the max and min values of the number of sunspots). Then, plot a \"new\" cosine waveform, y, with this vertical shift (remember that a vertical shift either adds or subtracts to the entire function). Color 4. should be red for this plot. Verify that the \"new\" cosine wave has a midpoint value that runs through the midpoint of the original data 5. To adjust the amplitude (in order to start moving the function closer to \"best fit\" the original data) you need to the amplitude adjust, aa, as half the difference between the maximum and minimum values of the data calculate the amplitude adjust, aa, as half the difference between the maximum and (again, the max. and min values of the number of sunspots). Then, use the color green to plot ya - the cosine function with the change in amplitude, aa, and vertical shift, vs. This latest plot should be close to fitting the original data but still off in frequency and phase shift. Now, calculate the frequency of the data, frequency, by finding the average year difference between all the maximum values in the data. As you can see in the data, the maximums take place approximately every 11 6. Therefore, you need to find the locations of the maximum valued years. To do this, you will need to split up your data and find the maximum between the first 11 years, then the next 11 years and then the final S years (or up to the end of the data). Once you find the three maximum valued years (for example, 1977, 1988 and 1999), then you need to take an average (mean) of the difference between them (for example, calculate the average between 1999-1988 and 1988-1977). Once you find the average, then you have calculated the period period, and you can find the frequency by dividing 2(pi) by the period. Using color blue, plot yf, the \"newest\" cosine function (with the change in amplitude, aa, vertical shift, vs, and frequency change, frequency). 7. On this last part, you will need to adjust the horizontal shift or phase shift, phaseshifi. Remember that if the phase shift is positive, then the function moves in the negative horizontal direction. Determine what year the maximum value occurs and that should be the phase shift (for the given data set, the phase shift is to the right, which would mean it has a negative phase shift). Finally, use color black to plot yp, the \"phase shifted\" cosine

Solution

Answer:

Note: prog4data.mat is not given.

%import data

load (\'prog4data.dat\',\'year\',\'numss\');

figure

%plot year vs numss

plot(myYear,numss,\'m--\');

grid on

%label for axis

xlabel(\'YEAR\');

ylabel(\'NUMSS\');

title(\'SUNSPOTS\');

hold on

kk=myYear(1);

nn=myYear(length(myYear));

%x-value for normal cosine wave

x=kk:0.25:nn;

y=cos(x);

%plotting normal cosine wave

scatter(x,y,[],\'c\');

hold off

%minimum no of sun spots

minss=numss(1);

%maximum no.of sun spots

maxss=numss(1);

%loop to find the min and max sun spots

for aa=kk:nn

     if(numss(aa)<minss)

          minss=numss(aa);

     end

     if(numss(aa)>maxss)

          maxss=numss(aa);

     end

end

%find vertical shift

vs=(minss+maxss)/2;

len=length(myYear);

%create vector to hold the vertical shift

yv=zeros(len,1);

%loop to find the vertical shifted y-value

for aa=1:len

     yv(aa) = cos(myYear(aa))+vs;

end

%plot vertical shifted cosine wave form

plot(myYear,yv, \'r--\');

xlabel(\'Year\');

ylabel(\'NUMSS\');

title(\'Cosine wave with vertical shift\');

%find amplitude

[val1 id1]=max(numss);

[val2 id2]=min(numss);

aa=0;

if(id1>id2)

     aa=(id1-id2)/2;

else

     aa=(id2-id1)/2;

end

ya=zeros(len,1);

%loop to find the ya

for bb=1:len

     ya(bb) = (aa*cos(myYear(aa)))+vs;

end

%plot vertical shifted cosine wave form

plot(myYear,ya, \'g--\');

xlabel(\'Year\');

ylabel(\'NUMSS\');

title(\'Cosine wave with new amplitude\');

max1=numss(1);

for bb=1:11

     t=numss(bb);

     if(t>max1)

          max1=t;

     end

end

max2=numss(12);

for bb=12:22

     t=numss(bb);

     if(t>max2)

          max2=t;

     end

end

max3=numss(23);

for bb=23:30

     t=numss(bb);

     if(t>max3)

          max3=t;

     end

end

d1=abs(max1-max2);

d2=abs(max2-max3);

period=(d1+d2)/2;

frequency=(2*pi)/period;

yf=zeros(len,1);

%loop to find the ya

for bb=1:len

     yf(bb) = (aa*cos(frequency *myYear(aa)))+vs;

end

%plot vertical shifted cosine wave form

plot(myYear,yf, \'b--\');

xlabel(\'Year\');

ylabel(\'NUMSS\');

title(\'Cosine wave with new frequency\');

phaseshift=myYear(id1) %maximum value occurs at id1

yp=zeros(len,1);

%loop to find the ya

for bb=1:len

     yp(bb) = (aa*cos(frequency *(myYear(aa)-phaseshift)))+vs;

end

%plot vertical shifted cosine wave form

plot(myYear,yp, \'k--\');

xlabel(\'Year\');

ylabel(\'NUMSS\');

title(\'Cosine wave with new phase shift\');

 ENGR 215 Fall 2016 Programming with MATLAB Programming Assignment 4 (Due Date: Oct. 23, 2016) (For this assignment, you will need to email a MATLAB script call
 ENGR 215 Fall 2016 Programming with MATLAB Programming Assignment 4 (Due Date: Oct. 23, 2016) (For this assignment, you will need to email a MATLAB script call
 ENGR 215 Fall 2016 Programming with MATLAB Programming Assignment 4 (Due Date: Oct. 23, 2016) (For this assignment, you will need to email a MATLAB script call
 ENGR 215 Fall 2016 Programming with MATLAB Programming Assignment 4 (Due Date: Oct. 23, 2016) (For this assignment, you will need to email a MATLAB script call
 ENGR 215 Fall 2016 Programming with MATLAB Programming Assignment 4 (Due Date: Oct. 23, 2016) (For this assignment, you will need to email a MATLAB script call

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site