Using matlab to create and graph distributions - matlab

How would I write the code to a six-sided dice are rolled and the two numbers showing are added to produce a sum between 2 and 12? Then plotting it

This code is for 10 observation. You can change it according to your criteria.
for i =1:1:10
first_no = randi([1 6],1);
second_no = randi([1 6],1);
if second_no == first_no
second_no = randi([1 6],1);
end
sum(i) = first_no + second_no
no(i) = 1
end
figure;
plot(no, sum)

You just need to define two variables that take the random values between 1-6. At the start, you can have the option of the number of observations. See the code below:
no_obs = 5;
for i=1:no_obs
num1 = randi([1 6],1);
num2 = randi([1 6],1);
sum(i) = num1 + num2;
end
display(sum);
figure;
plot(sum)

Related

How to improve this function?

The problem is there are c no. of firms bidding on p no. of projects. The winning bidders should collectively have the lowest cost on the client. Each firm can win a maximum of 2 projects.
I have written this code. It works, but takes forever to produce the result, and is very inefficient.
==========================================================================
function FINANCIAL_RESULTS
clear all; clc;
%This Matlab Program aims to select a large number of random combinations,
%filter those with more than two allocations per firm, and select the
%lowest price.
%number of companies
c = 7;
%number of projects
p = 9;
%max number of projects per company
lim = 2;
%upper and lower random limits
a = 1;
b = c;
%Results Matrix: each row represents the bidding price of one firm on all projects
Results = [382200,444050,725200,279250,750800,190200,528150,297700,297700;339040,393420,649520,243960,695760,157960,454550,259700,256980;388032,499002,721216,9999999,773184,204114,512148,293608,300934;385220,453130,737860,287480,9999999,188960,506690,274260,285670;351600,9999999,9999999,276150,722400,9999999,484150,266000,281400;404776,476444,722540,311634,778424,210776,521520,413130,442160;333400,403810,614720,232200,656140,165660,9999999,274180,274180];
Output = zeros(1,p+1);
n=1;
i=1;
for i = 1:10000000
rndm = round(a + (b-a).*rand(1,p));
%random checker with the criteria (max 2 allocations)
Check = tabulate(rndm);
if max(Check(:,2)) > lim
continue
end
Output(n,1:end-1) = rndm;
%Cumulative addition of random results
for k = 1:p
Output(n,end) = Output(n,end) + Results(rndm(k),k);
end
n = n+1;
end
disp(Results);
[Min_pay,Indx] = min(Output(:,end));
disp(Output(Indx,:));
%You know the program is done when Handel plays
load handel
sound(y,Fs);
%Done !
end
Since the first dimension is much greater than the second dimension it would be more efficient to perform loop along the second dimension:
i = 10000000;
rndm = round(a + (b-a) .* rand(i, p));
Check = zeros(size(rndm, 1), 1);
for k = 1:p
Check = max(Check, sum(rndm == k, 2));
end
rndm = rndm(Check <= lim, :);
OutputEnd = zeros(size(rndm, 1), 1);
for k = 1:p
OutputEnd = OutputEnd + Results(rndm(:, k), k);
end
Output = [rndm OutputEnd];
Note that if the compute has a limited memory put the above code inside a loop and concatenate the results of iterations to produce the final result:
n = 10;
Outputc = cell(n, 1);
for j = 1:n
i = 1000000;
....
Outputc{j} = Output;
end
Output = cat(1, Outputc{:});

How to plot Data From an Excel File in MATLAB? fplot Please

I am trying to plot data from an Excel file. I am reading from the second sheet, which has names of machines (machine numbers, but it needs to be flexible), on the top and then numbers underneath.
Machine1
1
2
3
4
5
6
etc.
The first row, which has the names, needs to be the name of each graph.
Here is what I have so far. Also, please no advanced functions.
clear variables
clc
close all
i = 0;
n = 1;
% All the code to do the first part of the graphing
inputFile = xlsread('machineOutput.xlsx', 'Sheet2');
[rows, cols] = size(inputFile);
for c = 1: cols
clear tempVar;
clear tempName;
count = 0;
i = 1;
for r = 1: rows
if(isnan(inputFile(r, c)) == 1)
inputFile(r, c) = 0;
end
tempVar{n} = inputFile(r, c);
tempName{i} = inputFile(r, c);
end
fplot(tempVar, [-2 2])
title(tempName)
grid on
xlabel('Number of Days')
ylabel('All machines')
figure
xlim tight
n = n + 1;
end

Attempting Tridiagonal Gauss-Jordan Elimination Matlab

As you probably guessed from the title, I'm attempting to do tridiagonal GaussJordan elimination. I'm trying to do it without the default solver. My answers aren't coming out correct and I need some assistance as to where the error is in my code.
I'm getting different values for A/b and x, using the code I have.
n = 4;
#Range for diagonals
ranged = [15 20];
rangesd = [1 5];
#Vectors for tridiagonal matrix
supd = randi(rangesd,[1,n-1]);
d = randi(ranged,[1,n]);
subd = randi(rangesd,[1,n-1]);
#Creates system Ax+b
A = diag(supd,1) + diag(d,0) + diag(subd,-1)
b = randi(10,[1,n])
#Uses default solver
y = A/b
function x = naive_gauss(A,b);
#Forward elimination
for k=1:n-1
for i=k+1:n
xmult = A(i,k)/A(k,k);
for j=k+1:n
A(i,j) = A(i,j)-xmult*A(k,j);
end
b(i) = b(i)-xmult*b(k);
end
end
#Backwards elimination
x(n) = b(n)/A(n,n);
for i=n-1:-1:1
sum = b(i);
for j=i+1:n
sum = sum-A(i,j)*x(j);
end
x(i) = sum/A(i,i)
end
end
x
Your algorithm is correct. The value of y that you compare against is wrong.
you have y=A/b, but the correct syntax to get the solution of the system should be y=A\b.

Simple Matlab loop creation

Good morning,
I have a doubt about a loop. I think it's real simple but I don't get how to do it. I'm going to try to simplify the question.
x= [... ; 106; 112; 111]
param= [1.2 ; 1.5; 1.7]
What I need to do is the following. Create three new values, by doing this:
1st loop:
> y(k) = a1*x(k-1) - a2*x(k-2) - a3*x(k-3)
> y(k) = (1.2*111)+(1.5*112)+( 1.7*106) =
> y(K) = 481, 4 result of the new value
2nd loop:
x= [... ; 106; 112; 111; 481,4] % this is the new added value to the vector:
y(k) = a1*x(k-1) - a2*x(k-2) - a3*x(k-3)
y(k) = (1.2*481,4)+(1.5 *111)+( 1.7*112) =
y(K) = result of the 2 new value
The routine consists in using always the param values 'by order' and multiply the x vector using penultimate value, then the antepenultimate and the following. I don't know how to manage it because it has to create three new times.
Any advice would be appreciated! :) Thanks in advance
You can use only y (or x) no need for both, as you just add all y in the end of x. Here is a simple solution for adding another N values:
N = 103;
y = zeros(N,1);
y(1:3) = [106 112 111]; % this is the end of your x
param = [1.2 ; 1.5; 1.7];
for k = 4:N
y(k) = (param(1).*y(k-1))+(param(2).*y(k-2))+(param(3).*y(k-3));
end

Jacobi solver going into an infinite loop

I can't seem to find a fix to my infinite loop. I have coded a Jacobi solver to solve a system of linear equations.
Here is my code:
function [x, i] = Jacobi(A, b, x0, TOL)
[m n] = size(A);
i = 0;
x = [0;0;0];
while (true)
i =1;
for r=1:m
sum = 0;
for c=1:n
if r~=c
sum = sum + A(r,c)*x(c);
else
x(r) = (-sum + b(r))/A(r,c);
end
x(r) = (-sum + b(r))/A(r,c);
xxx end xxx
end
if abs(norm(x) - norm(x0)) < TOL;
break
end
x0 = x;
i = i + 1;
end
When I terminate the code it ends at the line with xxx
The reason why your code isn't working is due to the logic of your if statements inside your for loops. Specifically, you need to accumulate all values for a particular row that don't belong to the diagonal of that row first. Once that's done, you then perform the division. You also need to make sure that you're dividing by the diagonal coefficient of A for that row you're concentrating on, which corresponds to the component of x you're trying to solve for. You also need to remove the i=1 statement at the beginning of your loop. You're resetting i each time.
In other words:
function [x, i] = Jacobi(A, b, x0, TOL)
[m n] = size(A);
i = 0;
x = [0;0;0];
while (true)
for r=1:m
sum = 0;
for c=1:n
if r==c %// NEW
continue;
end
sum = sum + A(r,c)*x(c); %// NEW
end
x(r) = (-sum + b(r))/A(r,r); %// CHANGE
end
if abs(norm(x) - norm(x0)) < TOL;
break
end
x0 = x;
i = i + 1;
end
Example use:
A = [6 1 1; 1 5 3; 0 2 4]
b = [1 2 3].';
[x,i] = Jacobi(A, b, [0;0;0], 1e-10)
x =
0.048780487792648
-0.085365853612062
0.792682926806031
i =
20
This means it took 20 iterations to achieve a solution with tolerance 1e-10. Compare this with MATLAB's built-in inverse:
x2 = A \ b
x2 =
0.048780487804878
-0.085365853658537
0.792682926829268
As you can see, I specified a tolerance of 1e-10, which means we are guaranteed to have 10 decimal places of accuracy. We can certainly see 10 decimal places of accuracy between what Jacobi gives us with what MATLAB gives us built-in.