Trying to get S(100) value several times

2 visualizaciones (últimos 30 días)
Tim Deutman
Tim Deutman el 7 de Oct. de 2021
Respondida: Image Analyst el 7 de Oct. de 2021
clc
T = 1;
S(1) = 3400;
sigma = 0.0003;
L = 10;
delta_t = T/L;
n = linspace(0,L,L^2);
mu = 0;
y = randn(1,L^2);
M = 10;
for i = 1:M
for t = 1:length(n)
S(t+1) = S(t) + mu*delta_t*S(t) + sqrt(sigma)*sqrt(delta_t)*y(t)*S(t);
end
S(100)
end
Hi guys, I have come across a problem that I can't seem to solve. I am a beginner to Matlab and am trying to get several S(100) values back so I can get the mean of the calculated S(100). But Matlab gives me back the same S(100) 10 times. I want 10 different values of S(100). How do i code this? thanks in advance.

Respuestas (2)

Star Strider
Star Strider el 7 de Oct. de 2021
Editada: Star Strider el 7 de Oct. de 2021
The value after 100 iterations of the ‘t’ loop will be the last value of ‘S’, so subscript it with the index of the outer loop to create the matrix —
T = 1;
S(1) = 3400;
sigma = 0.0003;
L = 10;
delta_t = T/L;
n = linspace(0,L,L^2);
mu = 0;
y = randn(1,L^2);
M = 10;
for i = 1:M
for t = 1:length(n)
S(t+1) = S(t) + mu*delta_t*S(t) + sqrt(sigma)*sqrt(delta_t)*y(t)*S(t);
end
S100(i,:) = S(end);
end
S100
S100 = 10×1
1.0e+03 * 3.2701 3.2701 3.2701 3.2701 3.2701 3.2701 3.2701 3.2701 3.2701 3.2701
S100mean = mean(S100)
S100mean = 3.2701e+03
S100sd = std(S100)
S100sd = 0
.
  3 comentarios
Star Strider
Star Strider el 7 de Oct. de 2021
The only variation in ‘S’ in each iteration are due to ‘y’ and the previous value of ‘S’. In each iteration, 101 values of ‘S’ are calculated.
What constitutes a ‘unique value of S(100)’ when ‘S’ is a 101-element vector?
There are several ways of creating a scalar value for ‘S’ in each iteration, such as sum, prod, and others, including selecting one element from each ‘S’ vector. (Perhaps the last?)
How do you want to define it?
.
Steven Lord
Steven Lord el 7 de Oct. de 2021
Nothing in the expression inside the nested loop depends on i. [The only place that character exists is as part of the identifier "sigma".] Nor does that expression involve any random numbers. Because of that each time that inner loop gets executed it ought to generate the same result in the end. If it doesn't, something odd is going on.
So what do you mean by "10 unique values of S100"?

Iniciar sesión para comentar.


Image Analyst
Image Analyst el 7 de Oct. de 2021
Perhaps you wanted to get a new random set of y values at each experiment (iteration) like this:
% Initialization steps.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
% Define experimental parameters:
T = 1;
S(1) = 3400;
sigma = 0.0003;
L = 10;
delta_t = T/L;
n = linspace(0,L, L^2);
mu = 0;
y = randn(1, L^2);
numExperimentsM = 10;
% Declare output for saving
s100 = zeros(1, numExperimentsM);
% Now run the experiments:
for k = 1 : numExperimentsM
% Get a new set of random y values on each experiment (iteration).
y = randn(1,L^2);
% Make an array of S's with the new y, starting with the same S(1) each time (which never gets changed).
for t = 1:length(n)
S(t+1) = S(t) + mu*delta_t*S(t) + sqrt(sigma)*sqrt(delta_t)*y(t)*S(t);
end
% At this point, S is 101 elements long, because we went to length(n) instead of (length(n) - 1).
% Print out the 100th element
fprintf('At experiment #%d, S(100) = %f.\n', k, S(100))
% Save this particular S(100) value into the vector s100
s100(k) = S(100);
end
% Get the mean of all the S(100) elements
meanS100 = mean(s100);
fprintf('The mean S(100) after %d experiments = %f.\n', numExperimentsM, meanS100);
% Plot them all
subplot(2, 1, 1);
bar(s100, 1)
grid on;
title('S(100) values vs. Experiment Number');
xlabel('Experiment Number');
ylabel('S(100) Value');
% Draw a line at the mea
yline(meanS100, 'Color', 'r', 'LineWidth', 2)
% Plot the distribution.
subplot(2, 1, 2);
h = histogram(s100)
grid on;
title('Histogram of S(100) values');
xlabel('S(100) Value');
ylabel('Count');
% Draw a line at the mea
xline(meanS100, 'Color', 'r', 'LineWidth', 2)
You see
At experiment #1, S(100) = 3434.185422.
At experiment #2, S(100) = 3231.380379.
At experiment #3, S(100) = 3249.098960.
At experiment #4, S(100) = 3349.556182.
At experiment #5, S(100) = 3649.605247.
At experiment #6, S(100) = 3753.271412.
At experiment #7, S(100) = 3281.038982.
At experiment #8, S(100) = 3631.479819.
At experiment #9, S(100) = 3575.251076.
At experiment #10, S(100) = 3406.949922.
The mean S(100) after 10 experiments = 3456.181740.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by