I am trying to decide the most efficient, yet most precise approach to calculate a value called R which can only take a value between 0 and 1. Right now I have the below function used in the below script, but I feel like I am doing this in a non-optimal way. Currently I get an answer and have to feed that answer (again) as the initial "guess" in order to get the (next) most optimal answer. Could I build a better recursion for this or perhaps use one of Matlab's solvers? Thanks!
The function:
function f = Rfind(p,u,R)
f = p .* (R.^u);
end
The script:
R = 0.999995753651217; % initial guess
matches = false;
while ~matches && R < 1
R = R + 0.0000000000000000001; % increment R for next guess
Jtotal = sum(Rfind(p,u,R)); % find R
if abs(Jtotal - R)*10000000000 < 5 % check precision of result
matches = true; % if R matches R fed to function, successful
end
end
Jtotal
What I'm trying to identify:
Find a value of R equal to the sum of array p times R to the power of array u. Array p and array u both have the same number of elements, i.e. 12 rows in 1 column each. My function calculates R for each p and u row and then increments its guess to find the next closest match. It stops once the precision limit has been met or the input R and output total are identical.
Example Data:
Array p
0.00000693
0.00000231
0.00001386
0.00000924
0.00041360
0.00461657
0.03085337
0.01595235
0.09614154
0.06832660
0.11103563
0.67262800
Array u
50000
500
50
25
10
7.5
5
3.5
2.5
1.25
1
0
Important: I need the best precision for this but I don't want it taking 10 minutes like it has with extensions of the above.
You can use fminbnd for this:
% first assign p and u
% define the function that you want to minimize:
Rfind = #(R) abs(sum(p.*(R.^u)) - R)
% set the tolerance to maximum:
options = optimset('TolX',eps);
% find the value between 0 to 1 that minimize the function Rfind:
[R, err] = fminbnd(Rfind,0,1,options)
and get (in a fraction of a second):
R =
0.999995761369809
err =
9.196743366857163e-11
Related
Looking within the xcorr function, most of it is pretty straightforward, except for one function within xcorr called "findTransformLength".
function m = findTransformLength(m)
m = 2*m;
while true
r = m;
for p = [2 3 5 7]
while (r > 1) && (mod(r, p) == 0)
r = r / p;
end
end
if r == 1
break;
end
m = m + 1;
end
With no comments, i fail to understand what this function is meant to acheive and what is the significance of p = [2 3 5 7]. Why those numbers specifically? Why not take a fixed FFT size instead? Is there a disadvantage(cause errors) to taking a fixed FFT size?
This part is used to get the integer closest to 2*m that can be written in the form:
Either:
m is already of this form, then the loop
for p = [2 3 5 7]
while (r > 1) && (mod(r, p) == 0)
r = r / p;
end
end
Will decrease r down to 1 and the break will be reached.
Or m has at least one other prime factor, and r will not reach 1. You go back to the look with m+1 and so on until you reach a number of the right form.
As per why they do this, you can see on the fft doc, in the Input arguments section:
n — Transform length [] (default) | nonnegative integer scalar
Transform length, specified as [] or a nonnegative integer scalar.
Specifying a positive integer scalar for the transform length can
increase the performance of fft. The length is typically specified as
a power of 2 or a value that can be factored into a product of small
prime numbers. If n is less than the length of the signal, then fft
ignores the remaining signal values past the nth entry and returns the
truncated result. If n is 0, then fft returns an empty matrix.
Example: n = 2^nextpow2(size(X,1))
I am writing a function to calculate a value of p.
The formula is p=r*q + pa. The pa value is column vector which i have got from the ode45 function. I have to change the value of q w.r.t time i.e from 0 to 1 sec q=500, from 1 to 2 sec q=0.
I have written this program:
function [p] = fom(t,pa)
r=0.007;
tin=1;
t=0:0.01:2;
if t <=tin
q = 600;
else t <= 2
q = 0;
end
[t,pa]=ode45('FOM1',[t],[0])
for i=[palv]
p=(r*q)+(i);
end
end
The problem is I am getting correct values after 1 sec in my output but before 1 sec where q is 0 values are not changing.
Is this if statement correct?
The problem is the following code snippet:
if t <=tin
q = 600;
else t <= 2
q = 0;
end
With t <= tin, you compare each element of vector t with tin. The result is again a vector, which contains 1 for all elements in t, which are smaller than tin, and 0 for all elements which are larger than tin, i.e.
[ 1 1 ... 1 1 0 0 ... 0 ]
According to the documentation, if evaluates the following:
An expression is true when its result is nonempty and contains only nonzero elements (logical or real numeric). Otherwise, the expression is false.
As the vector contains ones and zeros, i.e. not only nonzero elements, the this always evaluates to false and the else clause will be executed. q will therefore be a scalar, not a vector, which is always 0. To get a vector q, which is 600 for all t <= tin and zero otherwise, you can use
q = 600*(t <= tin);
which creates such a vector as printed above, and multiplies it by 600.
Now, a number of comments on your code:
Regarding the else: in the else, you can't have any other conditions, so t <= 2 will just be executed and printed to the console, but this does nothing, so you can leave that away.
You have t and pa as inputs to the function, but never use them. t is simply overwritten. So either leave t an input and don't overwrite it, or leave it away as input. Further, you can remove pa as input, as you calculate it using ode45.
I assume for i = palv is a typo and should be for i = pa.
In this for loop, by calling p=(r*q)+(i);, p gets overwritten in every iteration, i.e. the resulting p will be r*q plus the last element of pa.
You can create a vector p without a for loop using
p = r*q + pa
Everything together, the function becomes
function p = fom(t)
r=0.007;
tin=1;
q = 600 * (t <= tin);
[t,pa]=ode45('FOM1',[t],[0])
p = r*q + pa
end
and can be called e.g. by
t = 0:0.01:2
p = fom(t);
So I am very new to Matlab and I have been tasked with implementing LU factorization. I have to do it recursively because we are not allowed to use for loops in our code, and recursion will give us optimal marks. I have written this code. The code works for the first step, and does what it is supposed to, but for the next two steps, the matrix is not modified at all. I have this code:
function[L, U] = myLU(A,B, pos)
%A = Mtrix that becomes U
%B = Matrix that becomes L
tmp_L = B;
[x,y] = size(A);
if pos > x
L = B;
U = A;
return
else
pos %<-- just to see if it iterates through everything
[tmp_U,tmp_L] = elimMat(A,pos);
myLU(tmp_U,tmp_L, pos+1);
end
L = tmp_L;
U = tmp_U;
end
I where elimMat(A, pos) returns the elimination matrix for column pos. as well as another matrix, which will end up being the matrix of multipliers. What i tried doing is then finding the LU factorization of this matrix A. since elimMat returns L and U(this works, if i do it manually it works), i had to make a function that allows me to do it automatically without using a for loop. I thought i would do it recursively. What i ended up doing is adding another variable B to the function so that i can store intermediate values of my matrix obtained in each step and put it all together later.
So here is my question. Am i implementing the recursion wrong? and if i am how can i fix it? The other thing i wanted to ask is how can i implement this so i do not need variable B as an additional imput, and only use the existing variables, or variables previously defined, to find the solution? I would really like only two inputs in my function: The matrix name and the starting index.
here is elimMat if if helps:
function [M,L] = elimMat(A,k)
%find the size of the matrix
[x,y] = size(A);
tmp_mat = zeros(x,y);
%M = The current matrix we are working on for Elimination -> going to
%become U.
%L = The L part of the matrix we are working on. Contains all the
%multipliers. This is going to be our L matrix.
for i = 1:x
mult = A(i,k)/A(k,k);
if i > k
tmp_mat(i,k) = mult;
P = A(k,:)*mult;
A(i,:) = A(i,:)-P;
elseif i == k
tmp_mat(k,k) = 1;
end
end
M = A;
L = tmp_mat;
end
thanks for any feedback you can provide.
Here is the output: WHAT I GET VS what it should be:
[U = VS [U =
1 2 2 1 2 2
0 -4 -6 0 -4 -6
0 -2 -4] 0 0 2
L = VS [L=
1 0 0 1 0 0
4 0 0 4 1 0
4 0 0] 4 0.5 1
As you can see only the first column is changed
You forgot to catch the output of your recursive call:
[tmp_L, tmp_U] = myLU(tmp_U,tmp_L, pos+1);
Matlab passes variables by value, so a function cannot change its input variable itself (ok, it can, but it's tricky and unsafe).
Your original version didn't return the updated matrices, so the outermost function call encountered the myLU() call, let the recursion unfold and finish, and then went on to use tmp_L and tmp_U as returned from the very first call to elimMAT(A,1).
Note that you might want to standardize your functions such that they return U and L in the same order to avoid confusion.
I am trying out one of the matlab programming question.
Question:
Write a function called hulk that takes a row vector v as an input and
returns a matrix H whose first column consist of the elements of v,
whose second column consists of the squares of the elements of v, and
whose third column consists of the cubes of the elements v. For
example, if you call the function likes this, A = hulk(1:3) , then A
will be [ 1 1 1; 2 4 8; 3 9 27 ].
My Code:
function H = hulk(v)
H = [v; v.^2; v.^3];
size(H) = (n,3);
end
When I test my code using A = hulk(1:3), it throws an error on console.
Your function made an error for argument(s) 0
Am I doing something incorrect? Have I missed anything?
Remove the line size(H) = (n,3);
and add the line H = H';
Final code should be as follows
function H = hulk(v)
H = [v; v.^2; v.^3];
H = H';
end
Your code giving error in matlab editor on the size(H) = (n,3); line
That's why you should use the matlabeditor itself
For your future reference, you can very easily generalise this function in Matlab to allow the user to specify the number of cols in your output matrix. I also recommend that you make this function a bit more defensive by ensuring that you are working with column vectors even if your user submits a row vector.
function H = hulk(v, n)
%//Set default value for n to be 3 so it performs like your current function does when called with the same signature (i.e. only 1 argument)
if nargin < 2 %// nargin stands for "Number of ARGuments IN"
n = 3;
end if
%// Next force v to be a row vector using this trick (:)
%// Lastly use the very useful bsxfun function to perform the power calcs
H = bsxfun(#power, v(:), 1:n);
end
You could reduce the number of operations using cumprod. That way, each v.^k is computed as the previous v.^k times v:
function H = hulk(v, n)
H = cumprod(repmat(v,n,1),1);
The first input argument is the vector, and the second is the maximum exponent.
Just a simple question today. If I have an m*n matrix and I want to cycle through every value in it and apply a probability based function.
Basically, if the probability is p, then each value in the matrix has p chance of having the function applied to it.
I have the loop and the function itself all worked out, but I haven't found how to actually apply the probability itself.
Any advice would be greatly appreciated! Thanks in advance.
Here's your data matrix:
>> X = reshape(1:9, 3, 3);
and you want to (possibly) apply the following function to every element (note how I've vectorized it, so that it can take a matrix as an argument)
>> f = #(x) x.^2;
You want to apply the function with probability p
>> p = 0.25;
So generate some random numbers between 0 and 1, and see which ones are less than p
>> idx = rand(3,3) < p;
And now apply the function to the relevant indexes
>> X(idx) = f(X(idx));
Here's your result:
>> X
X =
1 16 7
2 5 64
3 6 81
The trick is that you can generate the random numbers first, and then apply the other formulas.
For example:
R = rand(m,n) < p
Now each value of R(row,col) corresponds to the outcome that you need to process your original matrix(row,col).
So I suggest applying your function to every cell and then setting the values to a default value based on some probability. So lets assume M is the result of applying to function to everycell:
default = NaN % Or 0 or whatever
p = 0.8;
M(rand(size(M)) > p) = default;
I think you might have to reshape m after this... not sure
M = reshape(M, m, n);