필터 지우기
필터 지우기

How can I add noise to linear graph

조회 수: 8 (최근 30일)
lucky_
lucky_ 2016년 1월 21일
댓글: jgg 2016년 1월 21일
I have 100 sample points and I'm using y=mx+c to create 1d line and add gaussian noise using randn the attached graph is the result that I got when I tried the first code and when I try the second code I get straight line I think there is something wrong with my plot but I'm not sure where ?
code1
sample = 100;
x = linspace(-5,5);
y= 1.6 * x +6;
dataset = randn(sample,2);
figure
plot(dataset,y)
axis([-8 7])
code2
sample = 100;
x = linspace(-5,5);
y= 1.6 * x +6;
dataset = y*randn(sample,2);
figure
plot(dataset)
axis([-8 7]);
this is the result that I want to get

채택된 답변

jgg
jgg 2016년 1월 21일
편집: jgg 2016년 1월 21일
You never added the random noise to your dataset:
sample = 100;
x = linspace(-5,5);
y= 1.6 * x +6;
r = randn(2,sample);
figure
plot(x,r(1,:)+y)
hold on
plot(x,r(2,:)+y)
The actual outcome value is y + r(i,:) not just y; you also are plotting it incorrectly before.
  댓글 수: 2
lucky_
lucky_ 2016년 1월 21일
Thanks that worked I'm still new to this so I wasn't sure .. is there function to plot fitted line
jgg
jgg 2016년 1월 21일
Yes. Let's code it up a little more clearly since you want to fit something too:
sample = 100;
x = linspace(-5,5);
y= 1.6 * x +6;
r = randn(2,sample);
y_noise = y + r(1,:);
X = [ones(1,length(x)); x];
beta = regress(y_noise',X');
y_fitted = beta(1) + x*beta(2);
figure
plot(x,y_noise)
hold on
plot(x,y_fitted)
This fits a linear regression curve to the noisy data using the regress function with an intercept. The coefficients are in beta.
If this answer solved your problem, please help others find it by accepting it.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Descriptive Statistics에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by