Face coloring in Matlab revisited - function

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.

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

How to invert the estimate of a function with multiple inputs, but only invert the function for a single input

I am trying to invert a function like one would invert an empirical cdf. If I wanted to invert an empirical cdf, I would write something like,
tau = 0.8
y=rnorm(1000,1)
[f,x]=ecdf(y)
q = interp1(f,x,tau,'next');
Instead, suppose that I have defined a function with multiple inputs, where all but the last input is based upon data. For example,
example_data= example_data_missingdatacdf(x1,x2,x3,scalar_delta)
I want to find the smallest value of delta such that
example_data_missingdatacdf(x1,x2,x3,scalar_delta)>= tau
What can I do? Thanks for any help in advance.
You would find the value of scalar_delta for which example_data_missingdatacdf(x1,x2,x3,scalar_delta) - tau = 0. Assuming the function is monotonously increasing, this is the smallest value that satisfies your requirement.
There are standard numerical techniques to find the zero crossing of a function. MATLAB implements such a technique in fzero.
This is how you’d use it:
fun = #(d) example_data_missingdatacdf(x1,x2,x3,d) - tau;
scalar_delta = fzero(fun, 0);
The 0 is the start point for the algorithm, your best guess. Given that the function is monotonic, it is not important to have a good guess, you will always find the only zero. But a good guess will make the algorithm converge faster.

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.

Is it possible to plot complex variable in wxMaxima or Octave?

For example , if I want to plot Sin(z) where z is a complex variable , how I will achieve it in either Octave or Maxima?
I don't know about Octave, but here is a message about that, with some code you can try in Maxima: https://www.ma.utexas.edu/pipermail/maxima/2007/006644.html
There may be more specific information for wxMaxima -- you can try their user forum: https://sourceforge.net/p/wxmaxima/discussion/435775/
(referring Octave 4.0.0)
How do you want to try to represent the output of the function? Plotting either the real or imaginary parts of the output can be done fairly simply using a 3-dimensional graph, where the x and y axes are the real and imaginary components of z, and the vertical axis is either the real or imaginary values of sin(z). Producing those are fairly simple in Octave. Here's a link to a script you can save and run to show an example.
Simply change the g = exp(f) line to g = sin(f).
Octave-help mailing list example
Note that the imaginary part plot is commented out. Just switch the # between the different plot commands if you want to see that part.
Now, are you instead looking for options to map the Z plane (z=x+iy) to the W plane (w=u+iv) and represent closed contours mapped by w=sin(z)? in that case you'll need to do parametric plotting as described on this FIT site. There is a link to his Matlab program at the bottom of the explanation that provides one method of using color coding to match z->w plane contour mapping.
Those m-files are written for Matlab, so a few things do not work, but the basic plotting is compatible with Octave 4.0.0. (the top level ss13.m file will fail on calls to flops and imwrite)
But, if you put your desired function in myfun13.m for f, df and d2f, (sin(z), cos(z), -sin(z) respectively), then run cvplot13, you'll get color maps showing the correspondence between z and w planes.
wxMaxima has a plot3d that can do it. Since the expression to plot is in terms of x and y, I plotted the function's magnitude with abs(f(x+%i*y)):
plot3d(abs((x+%i*y-3)*(x+%i*y-5)*(x+%i*y-6)), [x,2,7], [y,-1,1], [grid,100,100], [z,0,5])$

Octave specgram generates two diffent answers

I have a project where I have to recognize the frequency from an audio file. For this I use a single tone of 10 kHz to see if I can get it working.
Since I am pretty new to Octave, I tried this example with my own audio file.
I tried to understand what happens by doing some research to all functions.
My question here is; if I let specgram plot the figure when I do not specify it's output:
specgram(y,fftn,Fs,hanning(window),step);
it gives a line at 10kHz which is what I want.
But if I specify the output for the specgram function
[S,f,t]= specgram(y,fftn,Fs,hanning(window),step);
and let it plot, it plots the line at 18 kHz.
I figured it have to be in the inputs for the figure and I tried modifying these a bit, but every time I do that Octave gives an error.
I need the frequency as an given output, since I have to do some calculations with it, I figured I need to specify the frequency output.
This is the part of the code that specify the plot for the spectrogram:
step= fix(5*Fs/1000); % stepsize of the window
window= fix(90*Fs/1000); % window size
fftn =2^nextpow2(window); % Size of the FFT block
[S,f,t]= specgram(y,fftn,Fs,hanning(window),step);
S= abs(S(2:fftn*12000/Fs,:)); % Normalize the phase
S= S/max(S(:)); % Normalize the Energy
S= max(S, 10^(-40/10)); % Throw out values below -40 dB and above -3dB
S= min(S, 10^(-3/10));
figure
imagesc(t,f,(log(S)));
Can anyone help me here how to gain the frequency data from the audio file so I can use it in some calculations?
I have searched for answers already in the Octave manual for help and I tried it with various matlab sites. Also checked already many posts here such as:
How does Octave spectrogram 'specgram' from signal work?
Methodology of FFT for Matlab spectrogram / short time Fourier transform functions
P.S. Sorry for my bad English, it's not my native language
I found the answer myself, it turns out it is in this line of code:
S= abs(S(2:fftn*12000/Fs,:));
if I delete this line, the lines are placed on the right frequency in the figure. To me it looks like this line just takes a small space of the fft and replaces it with other frequencies but I'm not shure about that.