Plotting trendline and normally distributed numbers
Mostrar comentarios más antiguos
Hello guys,
I would need some help wtih the following problem.
I need to plot the calculated numbers (Nkonstant, Ntrend, Nsaisonal, NtrendSaisonal) as shown in my code.
So far i works out as I intended it to.
- But now i need to add the trendlines to the plots (I already calculated them in the for loop)
- and I need to change my variable e in a way that it represents normally distributed numbers with a mean 0 and a standard deviation of 20.
Would be so great if someone has got a solution to this.
Best regards,
Alex
clc;
close all;
clear all;
rng('default');
m = 100;
k = 1.2;
A = 50;
T = 13;
for t = 1:1:52;
e = randn();
Nkonstant = m + e;
Ntrend = m + k *t + e;
Nsaisonal = m + A*sin((2*t*pi)/T) + e;
NtrendSaisonal = m + k*t + A*sin((2*t*pi)/T) + e;
trendline1 = m;
trendline2 = m + k*t;
trendline3 = m + A*sin((2*t*pi)/T);
trendline4 = m + k*t + A*sin((2*t*pi)/T);
subplot(2,2,1);
plot(t, Nkonstant,'o');
hold on;
subplot(2,2,2);
plot(t, Ntrend,'o');
hold on;
subplot(2,2,3);
plot(t, Nsaisonal,'o');
hold on;
subplot(2,2,4);
plot(t, NtrendSaisonal,'o');
hold on;
end
Respuestas (3)
Adam Danz
el 29 de Mzo. de 2020
"I need to change my variable e in a way that it represents normally distributed numbers with a mean 0 and a standard deviation of 20. "
randn returns random number from a standard normal distribution with mean 0 and standard deviation or 1. To get a std of 20, just multiply by 20. Here's a demo
n = randn(1,100000) * 20;
std(n)
% ans =
% 20.015 % your value may differ slightly
" i need to add the trendlines to the plots (I already calculated them in the for loop)"
Tejaswini
el 5 de Oct. de 2026 a las 10:29
0 votos
clc;
close all;
clear all;
rng('default');
m = 100;
k = 1.2;
A = 50;
T = 13;
for t = 1:52
e = randn();
Nkonstant = m + e;
Ntrend = m + k*t + e;
Nsaisonal = m + A*sin((2*t*pi)/T) + e;
NtrendSaisonal = m + k*t + A*sin((2*t*pi)/T) + e;
trendline1 = m;
trendline2 = m + k*t;
trendline3 = m + A*sin((2*t*pi)/T);
trendline4 = m + k*t + A*sin((2*t*pi)/T);
subplot(2,2,1);
plot(t, Nkonstant, 'o');
hold on;
plot(t, trendline1, 'r-');
subplot(2,2,2);
plot(t, Ntrend, 'o');
hold on;
plot(t, trendline2, 'r-');
subplot(2,2,3);
plot(t, Nsaisonal, 'o');
hold on;
plot(t, trendline3, 'r-');
subplot(2,2,4);
plot(t, NtrendSaisonal, 'o');
hold on;
plot(t, trendline4, 'r-');
end
Tejaswini
el 5 de Oct. de 2026 a las 10:30
0 votos
clc;
close all;
clear all;
rng('default');
m = 100;
k = 1.2;
A = 50;
T = 13;
for t = 1:52
e = randn();
Nkonstant = m + e;
Ntrend = m + k*t + e;
Nsaisonal = m + A*sin((2*t*pi)/T) + e;
NtrendSaisonal = m + k*t + A*sin((2*t*pi)/T) + e;
trendline1 = m;
trendline2 = m + k*t;
trendline3 = m + A*sin((2*t*pi)/T);
trendline4 = m + k*t + A*sin((2*t*pi)/T);
subplot(2,2,1);
plot(t, Nkonstant, 'o');
hold on;
plot(t, trendline1, 'r-');
subplot(2,2,2);
plot(t, Ntrend, 'o');
hold on;
plot(t, trendline2, 'r-');
subplot(2,2,3);
plot(t, Nsaisonal, 'o');
hold on;
plot(t, trendline3, 'r-');
subplot(2,2,4);
plot(t, NtrendSaisonal, 'o');
hold on;
plot(t, trendline4, 'r-');
end
Categorías
Más información sobre 2-D and 3-D Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!