understanding the link between octave code and assignment equations - octave
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.
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
Octave: How to solve a one order differential equation with variable coefficients
I'm trying to solve a one order differential equation with variable coefficients of the following form: xdot(1)=a(t)*x(1)+b; where b=a constant and where a(t) = a time dependent function. I know that I can solve this equation by hand butt a(t) is a quite complex function. So, my problem is the following. a(t) is a function which I know its values from an experiment (I've got all the results in a file) --> a(t) is a vector (n x 1) which is a problem because x(1)= xdot(1)=a scalar. So, how could I solve this equation, with lsode ?
Possibly I have underestimated your problem, but the way I read it, you are asking to integrate a first order ODE. In general there are two ways to proceed,, implicit methods and explicit methods. Here is the crudest, but easiest to understand, method I can come up with: nt=101;a=-ones(1,nt);b=1/2;x=NaN*ones(1,nt);x(1)=pi;dt=0.01; for kt=2:nt dxdt=a(kt-1)*x(kt-1)+b; x(kt)=x(kt-1)+dxdt*dt; endfor plot(x) I have assumed a<0 so there is no tendency to blow up. You will want to set it equal to your observed values.
Boolean Expressions with nested NAND Gates?
I have a problem with some homework, for my Advanced Digital Design course, in which I have to create the truth table and find the Boolean expression for a provided circuit (image is linked below). I was able to create the truth table and I think find the Boolean Expression for the problem using the truth table that I created, but I don't think that this is the way that we are supposed to find the Boolean Expression. I was hoping that someone could share some insight on how to find the Boolean Expression without using the truth table. I would normally not have such an issue with this, but since there are 5 variables, and NAND gates, I am quite confused on how to simplify once I find it. I think that the outcome is something like: [(a(bcd)'e)']' when you look at the circuit, and not the table, but I am not entirely sure. I am also not sure on how to simplify this into a Boolean expression if this is right. Using the truth table to find the minterms I get y= m17+m19+m21+m22+m23+m25+m29 (which I am also not sure if it is right). And if I use a K-Chart to solve this, I end up with y = ab'e + a'ce + ab'cd, which seems like a legitimate simplified Boolean expression, but I have no clue if that is right. Since this question is worth 20 of the total 100 points, I could really use some help understanding how this works. Here is the image we were provided: Circuit: only circuit (a), not (b) Thank you!
I think that the outcome is something like: [(a(bcd)'e)']' Your first guess is correct. You just have to remark that whatever''=whatever f=[(a(bcd)'e)']' = a(bcd)'e Using de Morgan (bcd)' = b'+c'+d' Hence f=a(bcd)'e = ae(b'+c'+d') =ab'e + ac'e + ad'e which is minimal.
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.
Partition function in Matlab - is there something missing in my code?
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;