Is there anyway of obtaining the numerator and denominator in MATLAB without using numden() function? For example:
format rational
x=5/2;
I want to obtain 5 as num and 2 as den. Can you help me with this tricky problem.
How about
[N,D] = rat(2.5)
Otherwise, if you insist on doing it yourself, you can do something like
N = 2.5;
D=1; while (int64(N)~=N), N=N*10; D=D*10; end
g = gcd(N,D);
D = D/g;
N = N/g;
Related
I have some functions are depending on each ther , the functions are from this book page 136 http://www.cs.helsinki.fi/u/ahyvarin/papers/bookfinal_ICA.pdf .. I functions are presented below , How to write following functions in matlab ??
y(t) = W(t-1)*x(t)
h(t) = P(t-1)*y(t)
P(t)=(1/B)*Tri[P(t-1)-m(t)*h^T(t)]
m(t) = h(t)/(B+y^T(t))*h(t))
e(t) = x(t)-W^T(t-1)*y(t)
W(t) = W(t-1) + m(t)*e^T(t)
It is solving the weight matrix W(t) iteratively .. I tried to do like this in matlab but I did not work so may be you can advice to correct the code :
for i=1:10
e=randn(3,5000);
A=[1 0 0;-0.5 0.5 0;0.3 0.1 0.1];
x=A*e;
y(t) = W(t-1)*x(t)
h(t) = P(t-1)*y(t)
P(t)=(1/B)*Tri[P(t-1)-m(t)*h^T(t)]
m(t) = h(t)/(B+y^T(t))*h(t))
e(t) = x(t)-W^T(t-1)*y(t)
W(t) = W(t-1) + m(t)*e^T(t)
end
Thanks
Ok. I can't really understand what you want, but your code shows that you don't understand some moments. I will try to clarify some moments to you:
for i = 2:10
x = rand(3);
y = W(:,:,i-1)*x;
h = P(:,:,i-1)*y;
m=h/(1+y'*h);
P(:,:,i)=P(:,:,i-1)*m*h';
e=x-W(:,:,i-1)'*y;
W(:,:,i)=W(:,:,i-1)+m*e';
end
You must go something like this: 1. you calculate x and use it to calculate other functions.
2. all of them are matrices. So you need to define it first. For example y = ones(3) etc. 3.Thats not y^T or e^T. Its transposing. If you do not feel difference it's early for you to solve this task :)
And the last: Tri function will create a some kind of problems to you, but it's defined at 136 page.
P.S. i missed beta becouse of don't know what is it :)
I have the following problem:
max CEQ(w) s.t. w in (0,1) and I don't know anything about CEQ(w) except that is given by a fixed point equation of the form CEQ(w) = F(CEQ(w)). If I fix a w, I can solve the fixed point equation using the fzero function and obtain a value for CEQ. If I choose a different w, I get another value for CEQ. Thus, I could loop over all possible values of w and then choose the one that gives the highest CEQ. This seems bad programming though and I was wondering whether I can do this more efficient in MATLAB: I want to model the solution to my fixed point equation as a function of w but I don't know how to implement it.
To be more precise, here is a sample code:
clear all
clc
NoDraws = 1000000;
T_hat = 12;
mu = 0.0058;
variance = 0.0017;
rf = 0.0036;
sim_returns(:,T_hat/12) = T_hat*mu + sqrt(T_hat*variance)*randn(NoDraws,1);
A = 5;
kappa=1;
l=0;
theta = 1 - l*(kappa^(1-A) - 1) *(kappa>1);
CEQ_DA_0 = 1.1;
CEQ_opt = -1000;
w_opt = 0;
W_T = #(w) (1-w)*exp(rf*T_hat) + w*exp(rf*T_hat + sim_returns(:,T_hat/12));
for w=0.01:0.01:0.99
W=W_T(w);
fp = #(CEQ) theta*CEQ^(1-A)/(1-A) - mean( W.^(1-A)/(1-A)) + l*mean( ((kappa*CEQ)^(1-A)/(1-A) - W.^(1-A)/(1-A)) .* (W < kappa*CEQ));
CEQ_DA = fzero(fp,CEQ_DA_0);
if CEQ_DA > CEQ_opt
CEQ_opt = CEQ_DA;
w_opt = w;
end
end
That is, in the loop, I fix a w, solve the fixed point equation and store the value for CEQ. If some other w gives a bigger value for CEQ, the current optimal w gets replaced by that new w. what I would like to have (instead of the loop part) is something like this:
fp = #(CEQ,w) theta*CEQ^(1-A)/(1-A) - mean( W_T(w).^(1-A)/(1-A)) + l*mean( ((kappa*CEQ)^(1-A)/(1-A) - W_T(w).^(1-A)/(1-A)) .* (W_T(w) < kappa*CEQ));
CEQ_DA = #(w) fzero(fp,CEQ_DA_0);
[w_opt, fval]=fminbnd(CEQ_DA,0,1);
Your proposed solution is very close. In words, you're defining fp as a function of two arguments, and would like CEQ_DA to be a function of w, which solves fp for CEQ, with that given w. The only issue is that fzero doesn't know which parameter of fp to solve over, because it can't match anonymous function parameters and fp parameters by name.
The answer is yet one more anonymous function inside the fzero, to turn fp(CEP,w) into fp_w(CEP), which will be solveable for CEQ
CEQ_DA = #(w) fzero(#(CEQ) fp(CEQ, w),CEQ_DA_0);
I want to compute the following infinite sum in Matlab, for a given x and tau:
I tried the following code, given x=0.5 and tau=1:
symsum((8/pi/pi)*sin(n*pi*0.5)*sin(n*pi*0.5)*exp(-n*n*pi*pi)/n/n,1,inf)
But I get this:
(228155022448185*sum((exp(-pi^2*n^2)*((exp(-(pi*n*i)/2)*i)/2 - (exp((pi*n*i)/2)*i)/2)^2)/n^2, n == 1..Inf))/281474976710656
I want an explicit value, assuming the sum converges. What am I doing wrong? It seems like Matlab doesn't compute exp() when returning symsum results. How do I tell Matlab to compute evaluate the exponentials?
Convert to double
double(symsum(...))
Just to show you a different way, one that does not require the symbolic toolbox,
summ = 0;
summP = inf;
n = 1;
while abs(summP-summ) > 1e-16
summP = summ;
summ = summ + sin(n*pi*0.5)*sin(n*pi*0.5)*exp(-n*n*pi*pi)/n/n;
n = n + 1;
end
8/pi/pi * summ
which converges after just 1 iteration (pretty obvious, since exp(-4*6.28..)/n/n is so tiny, and sin(..) is always somewhere in [-1 1]). So given tau==1 and x==0.5, the infinite sum is essentially the value for n==1.
You should first define your variable "n" using syms. Then, you can include this variable in your symsum code.
Here's what I did:
syms n; AA = symsum((8/pi/pi)*sin(n*pi*0.5)*sin(n*pi*0.5)*exp(-n*n*pi*pi)/n/n,n,1,inf); BB = double(AA)
BB = 4.1925e-05
I have a complicated expression H which is derived from several other complicated intermediate step. I want to get the [N D] = numden(H).
The problem is that the expression H, N and D is not being "evaluated" and they are also not simplified and divided by common factors.
I am using syms x.
For example, I get
H = (27+81*(x^2-987605098534947/1125899906842624*x-...
That is just so crazy.. but clearly,
H = (27+81*(x^2-0.8772*x-...
how can I get it to evaluate to simplest form?
Thanks in advance!
EDIT: I found out the closest bet is using VPA
My own workaround:
[num den] = numden(H)
num = vpa(num, 4);
den = vpa(den, 4);
H = num/den;
repeat from 1 until desired num and den are obtained.
I want to calculate the function y(t) from the equation:
y(t) = -3t^2+5, t>=0
y(t) = 3t^2+5, t<0
for -9 <= t <= with the step-size 0.5
And I want to plot it by using MATLAB. I approach this question in two ways:
First
t=0:0.5:9
y=-3*t^2+5
t1=-0.00000000001:0.5:-9
y1=3*t^2+5
plot(t,y,t1,y1)
Second by using loop
t=-9:0.5:9
if(t>=0)
y=-3*(t.^2)+5
else
y=3.*(t.^2)+5
end
plot(t,y)
My problem is the two ways above seem not to give the same answer... Which one is the correct answer?
You can use the sign function to do this particular example a little easier:
t = -9:0.5:9;
y = -sign(t)*3.*t.^2 + 5;
plot(t,y);
In your first attempt, your t1 definition should be:
t1 = 0:-0.5:-9;
Note the minus sign on the increment.
Using a "loop" you seem to have left out the actual loop part. Try something like
t = -9:0.5:9;
for idx = 1:length(t)
if t(idx) <= 0
y(idx) = -3*(t(idx).^2)+5
etc.
Here's a more idiomatic solution that avoids SIGN for cases where that's not the only difference.
t = -9:0.5:9
y = -3*t.^2+5
y(t<0) = 3*t(t<0).^2+5
plot(t, y)