I wrote a simple program to help evaluate a probability problem. During 10 hours of the day, the probability of a certain event is different than for the rest of the day. To account for this , i'm trying to use the switch function but it's not working. The code is the following (I'm using MATLAB):
clear all
status = 0; %0 ventilador apagat, 1 encès.
nsim = 10000;
enceses = 0;
runtime = 0;
sampletime = 0;
minute = 0;
while (runtime < 10000 || enceses < 18000)
r = rand;
switch minute
case minute < 601
if (r < 0.1642)
status = 0;
else
runtime = runtime + 5/60;
if status == 0
status = 1;
enceses = enceses + 1;
else
end
end
sampletime = sampletime + 5/60;
minute = minute + 5;
case minute > 600
if (r < 0.801)
status = 0;
else
runtime = runtime + 5/60;
if status == 0
status = 1;
enceses = enceses + 1;
else
end
end
sampletime = sampletime + 5/60;
minute = minute + 5;
if minute == 1440
minute = 0;
end
end
end
enceses
runtime
sampletime
nsim does nothing for the moment
I analyzed the execution step by step and found that in the first loop the switch works as intended and goes into the first case but after that it enters neither, if i put an otherwise print ("error"), it just prints out a bunch of errors.
For your application, instead of switch-case, you can use if-elseif-end statements as follows:
if minute < 600
<some statements>
elseif minute >= 600
<another statements>
end
Related
As stated in title. I know that the probability of winning if one swaps doors in this problem should be roughly 66% but I seem to get answers implying a 33% success rate in cases where the player swaps as well as where the player does not swap. The variables associated with these two values are swapwinratio and noswapwinratio and these both consistently return values of roughly 0.33 at runtime. In tests, I've run sample numbers of 5000 to 10,000. Here is my code:
clc; clear all; close all;
numsamps = input("Enter number of samples to take. \n");
if(mod(numsamps, 1) || numsamps < 1)
error("Must be a positive integer number.");
end
wins = 0;
swapwins = 0;
noswapwins = 0;
swaps = 0;
noswaps = 0;
for i = 1:numsamps
doors = 1:3;
host = ceil(3 * rand());
player = ceil(3 * rand());
p1 = player;
doors(player) = 0;
goat = ceil(2 * rand());
for j = goat:3
if(doors(j) ~= 0)
doors(j) = 0;
break;
end
end
swap = floor(2 * rand());
if(swap)
ind = find(doors ~= 0);
player = doors(ind);
swaps = swaps + 1;
else
noswaps = noswaps + 1;
end
if(player == host)
wins = wins + 1;
if(swap)
swapwins = swapwins + 1;
else
noswapwins = noswapwins + 1;
end
end
end
swapwinratio = swapwins/swaps;
noswapwinratio = noswapwins/noswaps;
Sorry if I've missed anything; this is my first time posting to stackoverflow!
I have an if loop, which for the most part works correctly, but the first if loop part is supposed to provide my resulting matrix in zeroes as long as the loop is below a certain threshold (12,4 in my case) however, it does not do this at the moment.
I have tried to replace everything, and rewrite it. I am currently following a guide, so I do not have an extensive amount of knowledge on the subject. I have tried to manually calculate the matrix positions, which should result in zero, which it also amounted to.
T_A4 = 12.4
t_coA = 3
F_redA = 0.99
for i = 5:25
t_r = i;
I(i,1) = 10980*(i*60).^(-0.71);
for n = 1:60
t = n;
if n < T_A4
Q(t,i) = 0;
end
if T_A4 <= t < t_coA + T_A4
Q(t,i) = I(i,1)*F_redA*(t-T_A4)/t_coA;
end
if t_coA + T_A4 <= t < T_A4 + t_r
Q(t,i) = I(i,1)*F_redA;
end
if t_r + T_A4 <= t < T_A4 + t_coA + t_r
Q(t,i) = I(i,1)*F_redA*(1-(t-T_A4-t_r)/t_coA);
end
if t >= T_A4 + t_coA + t_r
Q(t,i) = 0;
end
end
end
Expected result should be a Q matrix where Q(1:12,:) = 0, however from Q(1:12,5:25) these all results in values that is not 0.
An example is Q(1,5) should result in 0, as n=1 is < 12.4. However my resulting Q(1,5) = 1225.1 :(
Tried to ask this at Matlab Central, and didn't get much replies.
This is the code:
for
... creates "cent"
end
e = 5 ;
per = zeros(e,e)
for u = 1:e
rsum = 0;
a = 0;
for p=1:e
u
p
Xdiff = 0;
Ydiff = 0;
Zdiff = 0;
Xdiff = (cent(u,1)-cent(:,1)).^2
Ydiff = (cent(u,2)-cent(:,2)).^2
Zdiff = (cent(u,3)-cent(:,3)).^2
a = (Xdiff + Ydiff + Zdiff).^0.5
rsum = cent(u,6) + cent(:,6) ;
if a == rsum(p)
per(p,u) = p ;
else
per(p,u) = 0 ;
end
end
end
The script runs just fine, and I get no error messages. However, i get no display of u and p, and per is returned as a matrix with only zeros if i create it before the first for loop. If i create per as shown over, between the first and second, it is not created at all. I thus think that the code stops after the first loop. Why?
Ok so I have been having a go at creating a prime number checker. I have been successful in making it work for a specific number. The code is here.
#To test if number is prime
prompt = input("number to test if prime: ");
n = prompt;
i = 2; #start of mod test
t = floor(sqrt(n));
counter = 0;
tic
for i = 2:t
if mod(n,i) == 0
disp('n is not prime')
break
else
counter = (counter + 1);
end
end
if counter == t-1
disp('n is prime')
end
toc
I then tried to make a program which would test a range of numbers. It's been successful for n=10, but when I go higher than that it doesn't seem to pick up the primes. I'm not sure where I'm going wrong.
#Want to test numbers 2:n if they're prime
prompt = input("max number to test: ");
n = prompt;
l = 2; #start of mod test
counter = 0;
tic
for i = 2:n #cycle to test 2 up to n
t = floor(sqrt(i)) #Only need to test up to root of number
for l = 2:t
if mod(i,l) == 0
break
else
counter = (counter + 1);
end
end
if counter == t-1 # if tested up to the root of the number, it must be prime
prime = sprintf('%d is prime', round(i));
disp(prime)
counter = 0;
end
end
toc
Any help in getting it to work for larger values would be greatly appreciated and also any ways to make the code more efficient. The top program can test 982451653 in 0.268 seconds on my laptop.
Just a way to linearize your algorithm:
n = 997 %test number
t1 = n./([2,3:2:n/2]);
t2 = t1 - round(t1);
res = sum(t2 == 0); %can n be divided ?
if res == 0
fprintf('%s','prime');
else
fprintf('%s','not prime');
end
I got help on the issue and found that 'counter' was constantly re-setting and needed to be moved up. Fixed code is here
#Want to test numbers 2:n if they're prime
prompt = input("max number to test: ");
n = prompt;
l = 2; #start of mod test
counter = 0;
tic
for i = 2:n #cycle to test 2 up to n
t = floor(sqrt(i)); #Only need to test up to root of number
counter = 0;
for l = 2:t
if mod(i,l) == 0
break
else
counter = (counter + 1);
end
end
if counter == t-1; # if tested up to the root of the number, it must be prime
prime = sprintf('%d is prime', round(i));
disp(prime)
end
end
toc
What is happening in the function below?
interpreting the output it appears to me that multiple recursions are occurring in parallel. Is that even possible (I didn't write any code to be executed parallely )
How can I fix this?
The top level function call:
for i = 1:n
chNo = cluster.no(i);
%if nodeArch.node(chNo).type ~= 'J'; %if type = J ie its an CH with no path to sink
nodeArch.node(chNo).dest = chNo; %default self loop
packetLength = nodeArch.node(chNo).data;
packetLength = packetLength * roundArch.aggrFactor;
path = num2str(i);
clusterModel= forwardPacket(clusterModel, packetLength, chNo, nodeArch.node(chNo).connections, path);
if nodeArch.node(chNo).dest == chNo
nodeArch.node(chNo).type = 'J'; %set node as isolated CH
end
%end
nodeArch.node(chNo).data = 0 ; %clearing local data
end
the recursive function :
function clusterModel = forwardPacket(clusterModel, packetLength, chNo, connections,path)
nodeArch = clusterModel.nodeArch;
netArch = clusterModel.netArch;
cluster = clusterModel.clusterNode;
zone = nodeArch.node(chNo).zone;
if zone == 1
distance = sqrt((nodeArch.node(chNo).x - netArch.Sink.x)^2 + (nodeArch.node(chNo).y - netArch.Sink.y)^2);
nodeArch.node(chNo).energy = afterTransmissionLoss( packetLength, netArch, distance, nodeArch.node(chNo).energy);
nodeArch.node(chNo).dest = 0; % ie sink
for j = 1:nodeArch.numNode
if connections(j) == 1;
netArch.Sink.connected(j) = 1;
%disp(num2str(j));
end
end
%This is the output block
path = strcat( path , ' > sink');
disp(path);
disp(netArch.Sink.data);
netArch.Sink.data = netArch.Sink.data + 1;
disp(netArch.Sink.data);
%end of output block
nodeArch.node(chNo).dest = 0;
else
n = length(cluster.no); % Number of CHs
for i = 1:n
destChNo = cluster.no(i);
if nodeArch.node(destChNo).zone == zone-1
distance = sqrt((nodeArch.node(chNo).x - nodeArch.node(destChNo).x)^2 + (nodeArch.node(chNo).y - nodeArch.node(destChNo).y)^2);
if distance <= netArch.Yard.nodeRange %check if CH is too far to send data
nodeArch.node(chNo).energy = afterTransmissionLoss( packetLength, netArch, distance, nodeArch.node(chNo).energy);
nodeArch.node(destChNo).energy = afterReceptionLoss( packetLength, netArch, nodeArch.node(destChNo).energy);
nodeArch.node(chNo).dest = destChNo;
%disp(strcat(num2str(destChNo),'>>' , num2str(destChNo) ));
path = strcat( path , ' > ' ,num2str(chNo));
clusterModel = forwardPacket(clusterModel, packetLength, destChNo, connections, path);
nodeArch.node(chNo).dest = destChNo;
break;
end
end
end
nodeArch.node(chNo).type = 'J'; %set node as isolated CH
end
clusterModel.netArch = netArch;
clusterModel.nodeArch = nodeArch;
end
the output:
2 >55 > sink
0
1
6 >72 > sink
0
1
7 >83 > sink
0
1
8 > sink
0
1
10 >106 >55 > sink
1
2
15 >186 > sink
1
2
16 >188 >55 > sink
1
2
17 >192 >330 > sink
1
2
21 > sink
1
2
22 > sink
2
3
26 >268 > sink
3
4
27 >271 > sink
3
4.....and so on
I figured out what's wrong
in Java or C
when we write
a=b; //where b is a structure/class
a is a reference of b.
in Matlab
a is another structure initialized with the values of b.
that was the issue.