Partition function in Matlab - is there something missing in my code? - function

Can anyone see if there is something wrong in my matlab code? My objective is to replicate this formula:
q can take value 1,2,3 and 5.
I constructed my vector Xt where each element are a cumulative sum of log(1+return) at each time (t) - for stock returns - first element is normalized to log(1).
Then to compute each element Sq(T,delta t) for the four values of q this is my matlab code:
for j=1:length(dt);
E=Xt(1:dt(j):end);
EE=diff(E(2:end));
EEE=diff(E(1:end-1));
Sqone(j)=sum(abs(EE-EEE).^1);
Sqtwo(j)=sum(abs(EE-EEE).^2);
Sqthree(j)=sum(abs(EE-EEE).^3);
Sqfive(j)=sum(abs(EE-EEE).^5); end;
Is there something wrong in the code above? I am asking this because I know there is something wrong since I am not getting the expected results. I am convinced that it is due to my code posted above.
the vector dt is a vector that goes from 1 to high number - depending on the size of Xt. But my vector dt is not the problem.
Thank you for all your help!

You are taking the difference twice. Once using diff and once using EE-EEE. The correct code is:
for j=1:length(dt);
E=Xt(1:dt(j):end);
EE=abs(diff(E));
Sqone(j)=sum(EE.^1);
Sqtwo(j)=sum(EE.^2);
Sqthree(j)=sum(EE.^3);
Sqfive(j)=sum(EE.^5); end;

Related

Amplitude Spectrum of a function

My question is related to plotting amplitude spectrum.
Problem 1: (I have solved it) I have to represent the following function as a discrete set of N=100 numbers separated by time increment of 1/N:
e(t) = 3sin2.1t + 2sin1.9t
I did it using stem function in matlab and plotted it.
Problem 2: (I have question about it) The next thing was to repeat the same above all, using dataset of 200 points with time increment of 1/N and 1/2N.
My question is a bit basic but I just want to clear if I am following the right path to solve my problem.
I want to ask that for problem 2, for both 1/N and 1/2N, should I use N=200 (as I believe it is separate problem)?
A few of my mates have suggested using N=100 for 1/N and N=200 for 1/2N.
which one is the right thing?
Any help will be highly appreciated. Thanks

understanding the link between octave code and assignment equations

I have been struggling with some questions from my study guide and really am stuck - I have asked the lecturer for help but his answer was literally "but it's been done for you" (referring to gauss_seidel code that was written) - to which I think he missed the point. I'm struggling to understand the actual question and how to approach it.
The first question reads as follows:
Define the 100x100 square matrix A and the column vector b by:
A(ij)=I(ij)+1/((i-j)2+1) b_(i)=1+2/i 1<=i j<=100
where I_(ij) is the 100x100 identity matrix (i.e 1 on the main diagonal and 0 everywhere else). Solve for x using both the Gauss-Seidel method and the A\b construct.
We have written the code for the gauss_seidel method, and i think i understand what it does mostly, however, i do not understand how the above question fits into the method. I was thinking that i'm supposed to do something like the following in the octave window then calling the gauss_seidel method:
>> A=eye(100,100);
>> b= (this is where i get slightly confused)... I've tried doing
>> for b=1:n;
>> b=1+(2/n);
That is question 1.
Question 2 I have given an answer and asked him about but he has not responded.
It reads: The Hilbert matrix is a square n x n matrix defined by:
H_(ij)n = 1/i+j+1
Define bn to be a column vector of dimension n, and with each element 1. Construct bn and then solve for x, Hn xn=bn in the cases n=4.
What i did here was simply:
>> b=ones (4,1);
>> x=hilb(4)\b;
and then it gave me the output of x values. Im not sure if what i did here was correct... since it doesnt mention using any method at all it just says solve for x.
Im not sure how to relate the lecturers reply to understanding the problem.
If you could help me by maybe letting me know what im missing or how i should be thinking about this, it would really help.
the gauss_seidel code looks like this:
function xnew=gauss_seidel(A,b,xold)
n=size(A)(1);
At=A;
xnew=xold;
for k=1:n
At(k,k)=0;
end
for k=1:n
xnew(k)=(b(k)-At(k,:)*xnew)/A(k,k);
end
endfunction
Ive been writing pseudo since last Monday and I am only a little bit clearer on what the code does.
A(ij)=I(ij)+1/((i-j)2+1), b(i)=1+2/i, 1<=i, j<=100
All this is really saying is that we have to create A and b in such a way that i>=1 and j<=100. After doing that, you simply solve using the Gauss Seidel method.
So we'd create b like this:
b=zeros(100,1);
for k=1:100
b(k) = 1+(2/k);
end
This will create a column vector with a size of 100x1 with all the values that satisfy b(i)=1+2/i where i (or in the code,'k') was greater or equal to 1.
Then to create A :
myMatrix=zeros(100,100);
for i=1:100
for j=1:100
myMatrix(i,j) = 1/(((i-j)^2) + 1);
end
end
A=eye(100) + myMatrix;
Now we have created A in such a way that it equals A(ij)=I(ij)+1/((i-j)2+1) where i was greater or equal to 1 & j was less than or equal to 100.
The rest of the question is basically asking to to solve for the values of x using the Gauss Seidel method.
So it be something like this :
y=iterative_linear_solve(A,b,x0,TOL,max_it,method);
Don't forget about creating x0 as the initial assumption, tolerance and max iterations etc.
In terms of question 2, you did exactly what I would have done. I think you're good with that.
I'm not too sure how to answer this :
If you could help me by maybe letting me know what im missing or how i
should be thinking about this, it would really help.
All I can really say is that you need to look at the problems in such a way that you see Ax=b. For example in the first question we started by making b, and then A. After that we simply applied the A\b construct or the Gauss Seidel method and got our answer.
And that's essentially what you did for the second question.
Lastly, are you a UNISA student by chance? I am, haha. I've been struggling with this on my own for a while. The study guides don't seem to give a lot of info.

Octave FWHM calculation

I am having some problem about calculating the FWHM of my data. Because the "fwhm" function in signal package results in a 100 times bigger value than i expected to get.
What i did is that,
Depending on the gaussian distribution function (you can find it on wikipedia) I produced some data. In this function you can give a specific sigma (RMS) value (FWHM=sigma*2.355). Here is that the script I wrote to understand the situation
x=10:0.01:40;
x0=25;
sigma=0.25;
y=(1/(sigma*sqrt(2*pi)))*exp(-((x-x0).^2)/(2*sigma^2));
z=fwhm(y)/2.355;
plot(x,y)
when I compared the results the output of "fwhm" function (24.999) is 100 times bigger than the one I used (0.25) in the function.
If you have any idea it will be very helpful.
Thanks in advance.
Your z is 100 times bigger because your steps in x are 1/100 (0.01). If you use fwhm(y) it is expected that the stepsize in x is 1. If not you have to specify that.
In your case you should do:
z=fwhm(x, y)/2.355
z = 0.24999
which matches your sigma

Face coloring in Matlab revisited

Using Mathematica I was able to create the following plot
Now I would like to switch to Matlab - which I am just starting to learn. I was able to create the triangulation with FL.vertices and FL.faces matrix and the patch function, that looks like this
faces=FV.faces;
facecolor = [.7 .7 .7];
patch('faces',faces,'vertices',FV.vertices,...
'facecolor',facecolor,'facealpha',0.8,'edgecolor',[.8.8.8]);
camlight('headlight','infinite');
daspect([1 1 1]); axis vis3d; axis off
material dull;
It produces a dull image:
Now, I have a function J that takes the matrix FL.vertices and returns a matrix of positive values. I would like to color the faces according to the values of J on vertices. Possibly interpolate along the faces. Edges can be, for now, as they are - to deal with later. After reading the documentation it is not clear to me how to accomplish this task. Do I need to find min and max of J manually? Or can Matlab do it automatically? It is OK for now to use one of Matlab's preset coloring schemes, something like a "temperature map" would do. At which point should I call my function J? How exactly it should be used with the patch command? I looked through the previous answers to a similar question, but still I am not able to figure out how to deal with my case. Any helping suggestion will be appreciated.
P.S.
OK. I think I did it with simple
FV.Cdata=sphere_jacobian(FV.vertices,1,1,0,1);
figure
Hp = patch('faces',FV.faces,'vertices',FV.vertices,...
'FaceVertexCData',FV.Cdata,'facecolor','interp','edgecolor',[.8 .8 .8]);
But I am not sure if min and max have been automatically computed and interpolated.
Here is what I believe to be the answer given by the poster, I will put it here so the question does not remain open.
OK. I think I did it with simple
FV.Cdata=sphere_jacobian(FV.vertices,1,1,0,1);
figure
Hp = patch('faces',FV.faces,'vertices',FV.vertices,...
'FaceVertexCData',FV.Cdata,'facecolor','interp','edgecolor',[.8 .8 .8]);
But I am not sure if min and max have been automatically computed and interpolated.
I did
colormap(hsv(3200));
and normalized my function:
jac = sphere_jacobian(FV.vertices,m);
minj = min(jac);
maxj = max(jac);
jac1 = (jac-minj*ones(size(jac)))/(maxj-minj);FV.Cdata=jac1;
figure Hp = patch('faces',FV.faces,'vertices',FV.vertices,... 'FaceVertexCData',FV.Cdata,'facecolor','interp','edgecolor',[.8 .8 .8]);
The result can be seen here.

matlab function which is a function of an intergral

I need to write my own function which has the form f(x,y)=Integrate(g(x,y,z),z from 0 to inf). so the code I used was:
function y=f(x,y)
g=#(z)exp(-z.^2)./(z.^x).*(z.^2+y.^2).^(x/2);% as a function of x,y and z
y=quadgk(g,0,inf)
and if I call it for a single value like f(x0,y0), it works but if I try to calculate something like f([1:10],y0), then the error message says that there is something wrong with the times and dimension. In principle I can use for loops but then my code slows down and takes forever. Is there any help I can get from you guys? or references?
I'm trying to avoid the for loop since in matlab it's much faster to use matrix computation than to use for loop. I wonder if there is any trick that I can take advantage of this feature.
Thanks for any help in advance,
Lynn
Perhaps you can try to transpose the interval, creating row based values instead of column based f([1:10]',y0). Otherwise something in your function might be wrong, for example to get x^y to work with lists as input, you have to prefix with a dot x.^y. The same for mulitply and division I think..
If loop is no problem for you, you should do something like:
function y2=f(x,y)
y2=zeros(size(x));
for n=1:numel(x)
g=#(z)exp(-z.^2)./(z.^x(n)).*(z.^2+y.^2).^(x(n)/2);% as a function of x,y and z
y2(n)=quadgk(g,0,inf)
end
The problem here is that quadk itself uses vectors as argument for g. Then you have in g somethink like z.^x, which is the power of two vectors that is only defined if z and x have the same dimension. But this is not what you want.
I assume that you want to evaluate the function for all arguments in x and that the output vector has the same dimension as x. But this does not seem to be possible since even this simple example
g=#(x)[x;x.^2]
quad(g,0,1)
does not work:
Error using quad (line 79)
The integrand function must return an output vector of the same length as the
input vector.
A similar error shows when using quadgk. The documentation also says that this routine works only for scalar functions and this is not surprising since an adaptive quadrature rule would in general use different points for each function to evaluate the integral.
You have to use quadvinstead, which can integrate vector valued functions. But this gives wrong results since your function is integrated in the interval [0,\infty).