create random data

2 visualizzazioni (ultimi 30 giorni)
jason
jason il 23 Feb 2012
function [Vw] = windspeed(t)
this creates a function that passes out the value Vw after taking in the value t right?
what i want is
F=[f1,f2,f3,f4 . . . fn]
do I just code
F=[1:100]
if fn is 100.
then A has the same number of integers but each one depends on F
So for
A=[a1,a2,a3 . . . an]
do I code A=k*F;
will this give me a value for A for every value of F(k is just a bunch of constants that I didnt bother writing out.)
then I have
theta1=rand(100);
does that give me 100 random numbers or one random number less than 100?
theta2=2*3.14*F*t
this should use each value of F and a time t that is passed into the program
theta=theta1+theta2; Y=cos(theta); Vw=A*Y';
does this calculate a matrix called Y built from the thetas and then multiply the matrix A from above by the transpose to give out a number of samples(100 due to the rand(100) line)
i thought I understood what was happening here but not really now that I'm struggling to code it.
The objective is to generate random fluctuations over a constant wind speed. The formula that A is calculated by is from the kaimal spectrum.
the last line of the program is basically stating the formula Vw(t) = sum (i=1 up to i=N) Ai*cos(2*pi*fi*t + thetai) So I have a forumula for A that depends on fi. The thetai is random and the theta2 is 2*pi*fi*t.
and the A and the F are meant to be declared. But to be honest my matlab programming skills are worse than I thought. does the program make 100 samples or does it make just one sample? and i must repeat it 100 times?
  1 Commento
the cyclist
the cyclist il 23 Feb 2012
Sorry I can't spend more time digging into your question right now, but I'll quickly mention that the syntax rand(100) will generate a 100x100 matrix of random values. To get a vector of 100 values, use rand(100,1) or rand(1,100).

Accedi per commentare.

Risposte (1)

Matt Tearle
Matt Tearle il 23 Feb 2012
One of the benefits of using MATLAB is that you can use it interactively to try these things out and see if they do what you want. Try playing with commands at the command line, before coding it all up in a function.
this creates a function that passes out the value Vw after taking in the value t right?
Correct.
F=[1:100]
This will create the array [1,2,3,...,100]. (The square brackets are actually unnecessary: F = 1:100; will do the job.)
do I code A=k*F; will this give me a value for A for every value of F(k is just a bunch of constants that I didnt bother writing out.)
A = k*F will indeed give you a value of A for each value of F. However, this is assuming a scalar value of k. If you have a different k for each value of F, then you want A = k.*F; (note the "."). If you have a different number of k values, then I don't know what you're trying to do mathematically (and neither does MATLAB).
theta1=rand(100); does that give me 100 random numbers or one random number less than 100?
doc rand => random numbers, uniformly distributed between 0 and 1. However rand(100) actually gives a 100-by-100 matrix of numbers.
this should use each value of F and a time t that is passed into the program
Yes, again, as long as t is a scalar.
theta=theta1+theta2
And now you're going to have problems. theta1 is a 100-by-100 matrix, theta2 is a vector (1-by-100). You will get a "matrix dimensions do not agree" error here.
does this calculate a matrix called Y built from the thetas and then multiply the matrix A from above by the transpose to give out a number of samples
Almost. If you change the definition of theta1 to rand(1,100), then A and Y will both be vectors. Then A*Y' is doing a matrix multiply of a row vector with a column vector (the transpose of Y). This is equivalent to the dot product, returning a single value.
From what I can interpret from your description, this is what you'd want. So it seems like the big problem is the definition of theta1. Try
theta1 = rand(1,100);
instead.
EDIT TO ADD: to be honest my matlab programming skills are worse than I thought

Community Treasure Hunt

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

Start Hunting!

Translated by