Matlab: generate two random numbers which are always different - matlab

I want to plot two grey squares, one of which is always darker than the other (for a test participant to decide in each trial which is darker).
I've set up the trials using
for N=1:20
w=rand(1)
z=rand(1)
% ...
end
And when I plot my squares I've set the color of one square using
'markerfacecolor', [w w w]
and set the second square similarly, using z.
The problem is: The two random numbers shouldn't be the same, because when this is the case the participant can't really decide which square is darker.
Can anyone help me to figure out how I can prevent the two random numbers being the same as each other within each loop?

"Shouldn't be the same" seems like a loose tolerance definition in this context, for instance would you allow w = 0.5, z = 0.50001? They would be pretty similar greys!
Let's define a tolerance, and find a random w and z
tol = 0.01; % forced difference between w and z
w = rand; % rand returns one value by default, don't have to use rand(1)
z = rand;
Now loop until z is either less than w-tol or greater than w+tol,
while abs(w-z) > tol
z = rand;
end
Note: You may want to add an iteration counter, so the while loop is ended after, say, 1000 attempts! Be aware that setting tol too large could cause this to take a lot longer.
Full example in the form of your example:
tol = 0.1; % larger tolerance
for N = 1:20
w = rand; z = rand;
while abs(w-z) > tol; z = rand; end;
% >> w = 0.8045
% >> z = 0.8169
% z = 0.7322
% z = 0.1895 % tolerance satisfied, stop while loop and continue
% Do your trial here...
end
Repeatability
If you wanted to do the exact same trials on multiple subjects, you would want the greys to be the same! Either run the randomising function once and store the output values for re-use, or reset the seed of the random number generator using rng

Since by definition the variable is "random", then you should not be naturally able to do that.
Given a large enough space from which you are picking the random value uniformly, it is pretty cheap computationally to simple add a while loop inside after obtaining w. Keep looping and choosing a new z until z is not equal to w. For you application the consequences are extremely negligible.

Related

how to replace the ode45 method with the runge-kutta in this matlab?

I tried everything and looked everywhere but can't find any solution for my question.
clc
clear all
%% Solving the Ordinary Differential Equation
G = 6.67408e-11; %Gravitational constant
M = 10; %Mass of the fixed object
r = 1; %Distance between the objects
tspan = [0 100000]; %Time Progression from 0 to 100000s
conditions = [1;0]; %y0= 1m apart, v0=0 m/s
F=#(t,y)var_r(y,G,M,r);
[t,y]=ode45(F,tspan,conditions); %ODE solver algorithm
%%part1: Plotting the Graph
% plot(t,y(:,1)); %Plotting the Graph
% xlabel('time (s)')
% ylabel('distance (m)')
%% part2: Animation of Results
plot(0,0,'b.','MarkerSize', 40);
hold on %to keep the first graph
for i=1:length(t)
k = plot(y(i,1),0,'r.','MarkerSize', 12);
pause(0.05);
axis([-1 2 -2 2]) %Defining the Axis
xlabel('X-axis') %X-Axis Label
ylabel('Y-axis') %Y-Axis Label
delete(k)
end
function yd=var_r(y,G,M,r) %function of variable r
g = (G*M)/(r + y(1))^2;
yd = [y(2); -g];
end
this is the code where I'm trying to replace the ode45 with the runge kutta method but its giving me errors. my runge kutta function:
function y = Runge_Kutta(f,x0,xf,y0,h)
n= (xf-x0)/h;
y=zeros(n+1,1);
x=(x0:h:xf);
y(1) = y0;
for i=1:n
k1 = f(x(i),y(i));
k2= f(x(i)+ h/2 , y(i) +h*(k1)/2);
y(i+1) = y(i)+(h*k2);
end
plot(x,y,'-.M')
legend('RKM')
title ('solution of y(x)');
xlabel('x');
ylabel('y(x)')
hold on
end
Before converting your ode45( ) solution to manually written RK scheme, it doesn't even look like your ode45( ) solution is correct. It appears you have a gravitational problem set up where the initial velocity is 0 so a small object will simply fall into a large mass M on a line (rectilinear motion), and that is why you have scalar position and velocity.
Going with this assumption, r is something you should be calculating on the fly, not using as a fixed input to the derivative function. E.g., I would have expected something like this:
F=#(t,y)var_r(y,G,M); % get rid of r
:
function yd=var_r(y,G,M) % function of current position y(1) and velocity y(2)
g = (G*M)/y(1)^2; % gravity accel based on current position
yd = [y(2); -g]; % assumes y(1) is positive, so acceleration is negative
end
The small object must start with a positive initial position for the derivative code to be valid as you have it written. As the small object falls into the large mass M, the above will only hold until it hits the surface or atmosphere of M. Or if you model M as a point mass, then this scheme will become increasingly difficult to integrate correctly because the acceleration becomes large without bound as the small mass gets very close to the point mass M. You would definitely need a variable step size approach in this case. The solution becomes invalid if it goes "through" mass M. In fact, once the speed gets too large the whole setup becomes invalid because of relativistic effects.
Maybe you could explain in more detail if your system is supposed to be set up this way, and what the purpose of the integration is. If it is really supposed to be a 2D or 3D problem, then more states need to be added.
For your manual Runge-Kutta code, you completely forgot to integrate the velocity so this is going to fail miserably. You need to carry a 2-element state from step to step, not a scalar as you are currently doing. E.g., something like this:
y=zeros(2,n+1); % 2-element state as columns of the y variable
x=(x0:h:xf);
y(:,1) = y0; % initial state is the first 2-element column
% change all the scalar y(i) to column y(:,i)
for i=1:n
k1 = f(x(i),y(:,i));
k2= f(x(i)+ h/2 , y(:,i) +h*(k1)/2);
y(:,i+1) = y(:,i)+(h*k2);
end
plot(x,y(1,:),'-.M') % plot the position part of the solution
This is all assuming the f that gets passed in is the same F you have in your original code.
y(1) is the first scalar element in the data structure of y (this counts in column-first order). You want to generate in y a list of column vectors, as your ODE is a system with state dimension 2. Thus you need to generate y with that format, y=zeros(length(x0),n+1); and then address the list entries as matrix columns y(:,1)=x0 and the same modification in every place where you extract or assign a list entry.
Matlab introduce various short-cuts that, if used consequently, lead to contradictions (I think the script-hater rant (german) is still valid in large parts). Essentially, unlike in other systems, Matlab gives direct access to the underlying data structure of matrices. y(k) is the element of the underlying flat array (that is interpreted column-first in Matlab like in Fortran, unlike, e.g., Numpy where it is row-first).
Only the two-index access is to the matrix with its dimensions. So y(:,k) is the k-th matrix column and y(k,:) the k-th matrix row. The single-index access is nice for row or column vectors, but leads immediately to problems when collecting such vectors in lists, as these lists are automatically matrices.

How to interpolate data in Matlab when the data is not monotonically increasing (but structured)?

I frequently use discretized curves described by vectors, say x and y, meaning that each point (x(k),y(k)) lies on the curve. Note that x and y are generally not monotonically increasing.
Then I need to represent the data differently, because I need to know the y-values where x equals a set of given values, i.e. I want a vector yr for a given vector xq such that (xq(k),yr(k)) are all good approximations of the original curve. So normally, interpolation would be used, however
yr = interp1(x,y,xq)
results in an error
The grid vectors are not strictly monotonic increasing.
How can I do this (in a nice way)? Note that I want to preserve the shape (connectivity between neighboring nodes) of the curve given by x and y.
Example Problem
Say you have data representing a circle in a 2D (x,y) space. What would you do if you needed the representation of that circle on a different x-grid?
PS: I will provide my current approach as an answer, but welcome other approaches, especially if they are "nicer" or "simpler"? (forgive me these subjective terms)
The current approach that I have works as follows. Basically, I do normal interpolation steps on pieces of the curve described by x and y.
% Determine whether xq is a column or row vector. This is used to make sure
% that xr and yr have the same orientation as xq.
if iscolumn(xq)
dim = 1;
else
dim = 2;
end
% Make sure that xq is unique and ascending
xq = unique(xq);
% Initialize xr and yr as empty arrays
[xr,yr] = deal([]);
% Loop over all [xk,xkp1]-intervals
for k = 1:length(x)-1
% Choose values from xq that are in [xk,xkp1] (or [xkp1,xk] if
% decreasing)
if x(k) <= x(k+1) % x is locally increasing
xradd = xq(x(k)<=xq & xq<=x(k+1));
else % x is locally decreasing
xradd = flip(xq(x(k+1)<=xq & xq<=x(k)));
% 'flip' ensures that xradd is also decreasing (to maintain order
% of x in xr)
end
% Perform local interpolation
yradd = interp1(x(k:k+1),y(k:k+1),xradd);
% Assemble xr and yr from local interpolations
xr = cat(dim,xr,xradd);
yr = cat(dim,yr,yradd);
end
This method works, but there may be other methods.

Plot a curve with each points specifc distance away (Matlab)

I am trying to draw a curve where each points are specific distance away from each other.
Now, blow shows pretty much what I wanted to do but I want sin like curve and not constant radius.
R = 50; %radius
Gap = 0.1; % gap between points
Curve = 180;
rad = 0;
n= pi*2*R*Curve/360/Gap; % n is length of arc
th = linspace( pi, rad ,n);
x = R*cos(th)+R;
y = R*sin(th)+100;
PathDB.Route1.x(1:1001,1)=0;
PathDB.Route1.y = (0:Gap:100)';
LengthY = length(PathDB.Route1.y);
PathDB.Route1.x(1001:1001+length(x)-1,1)=x ;
PathDB.Route1.y(LengthY:LengthY+length(y)-1) = y;
LengthX = length(PathDB.Route1.x);
LengthY = length(PathDB.Route1.y);
PathDB.Route1.x(LengthX:LengthX+1000,1)=PathDB.Route1.x(LengthX,1);
PathDB.Route1.y(LengthY:LengthY+1000,1)= (PathDB.Route1.y(LengthY,1):-Gap:0);
plot(PathDB.Route1.x, PathDB.Route1.y);
grid ;
axis equal
All I want to do is instead of perfect curve, I want to add sin like curve which are plotted by 0.1.
I'm sorry about my poor coding skills I hope you can understand and help me.
Any advice is appreciated!
Rui
If you want this to be robust, you'll probably need to use the Matlab Symbolic Toolbox. If you take a function f, and want to plot equidistant points along it's curve between a and b, you could do something like this (using sin(x) as the example function):
sym x;
f = sin(x);
g = diff(f); %derivative of f
b=5;
a=0;
%check syntax on this part
arc_length = int(sqrt(1+g^2)); %create function for arc_length
Sc = arc_length(b)-arc_length(a); %calculate total arc length
n = 30 %number of points to plot (not including b)
Sdist = Sc/n; %distance between points
xvals = zeros(1,(n+1)); %initialize array of x values, n+1 to include point at b
Next, we will want to iterate through the intervals to find equidistant points. To do this we take each S_ij where i represents the x value of the low end of the interval and j represents the high end. We start with i=a and stop when i=b. Using the inverse of our arc length equation we can solve for j. Sdist == S_ij => Sdist = arc_length(j) - arc_length(i) => Sdist+arc_length(i)=arc_length(j). Using this, we can compute: j = arc_length_inverse(Sdist+arc_length(i)). One way of implementing this would be as follows:
arc_length_inv = finverse(arc_length);
i=a
xvals(1)=i;
for ii=1:n
j = arc_length_inv(Sdist+arc_length(i)); %calculate high end
i = j %make high end new low end
xvals(ii+1)=i; %put new low end value in x array
end
With this array of x values, you can compute yvals=sin(xvals) and plot(xvals,yvals)
Some of this code probably needs tweaking but the math should be sound (the formula for arc length came from http://en.wikipedia.org/wiki/Arc_length). Mathematically, this should work for any function integrable on [a,b] and whose arc_length function has an inverse; that said, I do not know the capabilities of the int() and finverse() functions.
If you do not need robustness you can follow these steps for your specific function, doing the math by hand and entering the functions manually;

simulating a random walk in matlab

i have variable x that undergoes a random walk according to the following rules:
x(t+1)=x(t)-1; probability p=0.3
x(t+1)=x(t)-2; probability q=0.2
x(t+1)=x(t)+1; probability p=0.5
a) i have to create this variable initialized at zero and write a for loop for 100 steps and that runs 10000 times storing each final value in xfinal
b) i have to plot a probability distribution of xfinal (a histogram) choosing a bin size and normalization!!* i have to report the mean and variance of xfinal
c) i have to recreate the distribution by application of the central limit theorem and plot the probability distribution on the same plot!
help would be appreciated in telling me how to choose the bin size and normalize the histogram and how to attempt part c)
your help is much appreciated!!
p=0.3;
q=0.2;
s=0.5;
numberOfSteps = 100;
maxCount = 10000;
for count=1:maxCount
x=0;
for i = 1:numberOfSteps
random = rand(1, 1);
if random <=p
x=x-1;
elseif random<=(p+q)
x=x-2;
else
x=x+1;
end
end
xfinal(count) = x;
end
[f,x]=hist(xfinal,30);
figure(1)
bar(x,f/sum(f));
xlabel('xfinal')
ylabel('frequency')
mean = mean(xfinal)
variance = var(xfinal)
For the first question, check the help for hist on mathworks homepage
[nelements,centers] = hist(data,nbins);
You do not select the bin size, but the number of bins. nelements gives the elements per bin and center is all the bin centers. So to say, it would be the same to call
hist(data,nbins);
as
[nelements,centers] = hist(data,nbins);
plot(centers,nelements);
except that the representation is different (line or pile). To normalize, simply divide nelements with sum(nelements)
For c, here i.i.d. variables it actually is a difference if the variables are real or complex. However for real variables the central limit theorem in short tells you that for a large number of samples the distribution will limit the normal distribution. So if the samples are real, you simply asssumes a normal distribution, calculates the mean and variance and plots this as a normal distribution. If the variables are complex, then each of the variables will be normally distributed which means that you will have a rayleigh distribution instead.
Mathworks is deprecating hist that is being replaced with histogram.
more details in this link
You are not applying the PDF function as expected, the expression Y doesn't work
For instance Y does not have the right X-axis start stop points. And you are using x as input to Y while x already used as pivot inside the double for loop.
When I ran your code Y generates a single value, it is not a vector but just a scalar.
This
bar(x,f/sum(f));
bringing down all input values with sum(f) division? no need.
On attempting to overlap the ideal probability density function, often one has to do additional scaling, to have both real and ideal visually overlapped.
MATLAB can do the scaling for us, and no need to modify input data /sum(f).
With a dual plot using yyaxis
You also mixed variance and standard deviation.
Instead try something like this
y2=1 / sqrt(2*pi*var1)*exp(-(x2-m1).^2 / (2*var1))
ok, the following solves your question(s)
codehere
clear all;
close all;
clc
p=0.3; % thresholds
q=0.2;
s=0.5;
n_step=100;
max_cnt=10000;
n_bin=30; % histogram amount bins
xf=zeros(1,max_cnt);
for cnt=1:max_cnt % runs loop
x=0;
for i = 1:n_step % steps loop
t_rand1 = rand(1, 1);
if t_rand1 <=p
x=x-1;
elseif t_rand1<=(p+q)
x=x-2;
else
x=x+1;
end
end
xf(cnt) = x;
end
% [f,x]=hist(xf,n_bin);
hf1=figure(1)
ax1=gca
yyaxis left
hp1=histogram(xf,n_bin);
% bar(x,f/sum(f));
grid on
xlabel('xf')
ylabel('frequency')
m1 = mean(xf)
var1 = var(xf)
s1=var1^.5 % sigma
%applying central limit theorem %finding the mean
n_x2=1e3 % just enough points
min_x2=min(hp1.BinEdges)
max_x2=max(hp1.BinEdges)
% quite same as
min_x2=hp1.BinLimits(1)
max_x2=hp1.BinLimits(2)
x2=linspace(min_x2,max_x2,n_x2)
y2=1/sqrt(2*pi*var1)*exp(-(x2-m1).^2/(2*var1));
% hold(ax1,'on')
yyaxis right
plot(ax1,x2,y2,'r','LineWidth',2)
.
.
.
note I have not used these lines
% Xp=-1; Xq=-2; Xs=1; mu=Xp.*p+Xq.*q+Xs.*s;
% muN=n_step.*mu;
%
% sigma=(Xp).^2.*p+(Xq).^2.*q+(Xs).^2.s; % variance
% sigmaN=n_step.(sigma-(mu).^2);
People ususally call sigma to variance^.5
This supplied script is a good start point to now take it to wherever you need it to go.

Can someone explain how to graph this sum in MATLAB using contourf?

I'm going to start off by stating that, yes, this is homework (my first homework question on stackoverflow!). But I don't want you to solve it for me, I just want some guidance!
The equation in question is this:
I'm told to take N = 50, phi1 = 300, phi2 = 400, 0<=x<=1, and 0<=y<=1, and to let x and y be vectors of 100 equally spaced points, including the end points.
So the first thing I did was set those variables, and used x = linspace(0,1) and y = linspace(0,1) to make the correct vectors.
The question is Write a MATLAB script file called potential.m which calculates phi(x,y) and makes a filled contour plot versus x and y using the built-in function contourf (see the help command in MATLAB for examples). Make sure the figure is labeled properly. (Hint: the top and bottom portions of your domain should be hotter at about 400 degrees versus the left and right sides which should be at 300 degrees).
However, previously, I've calculated phi using either x or y as a constant. How am I supposed to calculate it where both are variables? Do I hold x steady, while running through every number in the vector of y, assigning that to a matrix, incrementing x to the next number in its vector after running through every value of y again and again? And then doing the same process, but slowly incrementing y instead?
If so, I've been using a loop that increments to the next row every time it loops through all 100 values. If I did it that way, I would end up with a massive matrix that has 200 rows and 100 columns. How would I use that in the linspace function?
If that's correct, this is how I'm finding my matrix:
clear
clc
format compact
x = linspace(0,1);
y = linspace(0,1);
N = 50;
phi1 = 300;
phi2 = 400;
phi = 0;
sum = 0;
for j = 1:100
for i = 1:100
for n = 1:N
sum = sum + ((2/(n*pi))*(((phi2-phi1)*(cos(n*pi)-1))/((exp(n*pi))-(exp(-n*pi))))*((1-(exp(-n*pi)))*(exp(n*pi*y(i)))+((exp(n*pi))-1)*(exp(-n*pi*y(i))))*sin(n*pi*x(j)));
end
phi(j,i) = phi1 - sum;
end
end
for j = 1:100
for i = 1:100
for n = 1:N
sum = sum + ((2/(n*pi))*(((phi2-phi1)*(cos(n*pi)-1))/((exp(n*pi))-(exp(-n*pi))))*((1-(exp(-n*pi)))*(exp(n*pi*y(j)))+((exp(n*pi))-1)*(exp(-n*pi*y(j))))*sin(n*pi*x(i)));
end
phi(j+100,i) = phi1 - sum;
end
end
This is the definition of contourf. I think I have to use contourf(X,Y,Z):
contourf(X,Y,Z), contourf(X,Y,Z,n), and contourf(X,Y,Z,v) draw filled contour plots of Z using X and Y to determine the x- and y-axis limits. When X and Y are matrices, they must be the same size as Z and must be monotonically increasing.
Here is the new code:
N = 50;
phi1 = 300;
phi2 = 400;
[x, y, n] = meshgrid(linspace(0,1),linspace(0,1),1:N)
f = phi1-((2./(n.*pi)).*(((phi2-phi1).*(cos(n.*pi)-1))./((exp(n.*pi))-(exp(-n.*pi)))).*((1-(exp(-1.*n.*pi))).*(exp(n.*pi.*y))+((exp(n.*pi))-1).*(exp(-1.*n.*pi.*y))).*sin(n.*pi.*x));
g = sum(f,3);
[x1,y1] = meshgrid(linspace(0,1),linspace(0,1));
contourf(x1,y1,g)
Vectorize the code. For example you can write f(x,y,n) with:
[x y n] = meshgrid(-1:0.1:1,-1:0.1:1,1:10);
f=exp(x.^2-y.^2).*n ;
f is a 3D matrix now just sum over the right dimension...
g=sum(f,3);
in order to use contourf, we'll take only the 2D part of x,y:
[x1 y1] = meshgrid(-1:0.1:1,-1:0.1:1);
contourf(x1,y1,g)
The reason your code takes so long to calculate the phi matrix is that you didn't pre-allocate the array. The error about size happens because phi is not 100x100. But instead of fixing those things, there's an even better way...
MATLAB is a MATrix LABoratory so this type of equation is pretty easy to compute using matrix operations. Hints:
Instead of looping over the values, rows, or columns of x and y, construct matrices to represent all the possible input combinations. Check out meshgrid for this.
You're still going to need a loop to sum over n = 1:N. But for each value of n, you can evaluate your equation for all x's and y's at once (using the matrices from hint 1). The key to making this work is using element-by-element operators, such as .* and ./.
Using matrix operations like this is The Matlab Way. Learn it and love it. (And get frustrated when using most other languages that don't have them.)
Good luck with your homework!