Info
이 질문은 마감되었습니다. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Using poly fit and polyval to graph a Gaussian function with 4 different degrees and 4 different noise factors
조회 수: 1 (최근 30일)
이전 댓글 표시
This problem deals with data fitting in the presence of noise. Write a function Gaussian.m which will generate a 1D Gaussian function of the form y=A.*exp((-(x-x_0).^2)./s);, where s is the spread of the Gaussian, A is a constant factor and the mean x_0. The inputs to the function should be a vector of values (x), A, s, and x_0. To test your function, plot the Gaussian corresponding to x= [-0.5:0.01:0.5-0.01], A = 100, s = 1, and x0= 0.
b. Add noise the Gaussian you generated above and plot the corresponding result. You may use the randn.m function in Matlab to generate a 100 random (noise) values between 0-1. Hence the new Gaussian function (Gnew = y + factor*noise) can be obtained. On the same graph, plot out Gnew for 4 different values of factor = {0.0, 0.5, 7.5, 15}.
c. Use the polyval and polyfit functions to fit polynomials of different degrees to the Gnew functions generated in (b) above. Fit 4 polynomials corresponding to degrees of 1, 2, 10, and 15 to Gnew for each value of factor (i.e. 0.0, 0.5, 7.5, 15). You should use the subplot function to generate 4 subplots on a single figure, each subplot corresponding to a different noise level.
My code:
% part a
A=100;
x=[-0.5:.01:.5-.01];
x_0=0;
s=1;
y=A.*exp((-(x-x_0).^2)./s);
figure(2)
G=Gaussian(A,x,x_0,s);
plot(x,G)
title('Gaussian with Given Values')
xlabel('x-axis')
ylabel('y-axis')
% part b
figure(3)
noise = rand(1,100);
Gnew1= y + 0*noise;
Gnew2= y + 0.5*noise;
Gnew3= y + 7.5*noise;
Gnew4= y + 15*noise;
plot(x,G,'k','linewidth',.5)
hold on
plot(x, Gnew1,':r')
plot(x, Gnew2,':g')
plot(x, Gnew3,':b')
plot(x, Gnew4,':m')
hold off
title('Gaussian with Noise')
xlabel('x-axis')
ylabel('y-axis')
I am having a lot of trouble understanding part c. also on how to make part b simpler and neater without having to use Gnew1, Gnew2, Gnew3, and Gnew4 and only having to use Gnew. I know it is something to do with loops but I can't figure it out.
댓글 수: 1
답변 (0개)
이 질문은 마감되었습니다.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!