MATLAB clc, clear x = -2*pi:pi/8:2*pi; y = sin(x); % replace 3 points with \"Not
ID: 3685997 • Letter: M
Question
MATLAB
clc, clear
x = -2*pi:pi/8:2*pi;
y = sin(x);
% replace 3 points with "Not A Number (NaN)"
y(10) = NaN;
y(15) = NaN;
y(4) = NaN;
a.) Matlab has a function called isnan which can be used to clean up the y values above. Save the cleaned up y values in goodY. This will leave us with 3 extra x values. Use the isnan() function and the y (not goodY) values to remove the corresponding x values.
b.) Create a subplot for three graphs and plot the goodY and goodX on the top graph.
c.) Matlab has a built in function interp1 (one not L). Use this function to generate another set of y values like this
newY = interp1(goodX, goodY, x) ß notice the original x
d.) Now plot the newY as the 2nd graph
e.) Just for comparison plot the old y as the 3 graph using asterisk only so that you can see the missing points.
Explanation / Answer
clc, clear
x = -2*pi:pi/8:2*pi;
subplot(2,2,3)
plot(x,'*');
title('old x')
y = sin(x);
subplot(2,2,4)
plot(y,'*');
title('old y');
% replace 3 points with "Not A Number (NaN)"
y(10) = NaN;
y(15) = NaN;
y(4) = NaN;
goodY= isnan(y);
subplot(2,2,2)
plot(goodY);
title('goody')
goodX= isnan(x);
subplot(2,2,1)
plot(goodX,'*');
title('goodx')
figure,
newY = interp1(goodX,goodY,x);
plot(newY,'*');
title('newy')
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.