coding for recursive formula

3 visualizaciones (últimos 30 días)
YOGESHWARI PATEL
YOGESHWARI PATEL el 1 de Jun. de 2020
Respondida: John D'Errico el 14 de Jun. de 2020
I write the code for the recurive formula first code i wrote give me wrong answer.The code is as follows:
c1(1)=0;
c2(1)=0;
c3(1)=0;
c4(1)=0;
for k=1:1:10
if k==1
B=1;
else
B=0;
end
c1(k+1)=(((B*(1/120))+(18/120)*(c2(k)-c1(k)))-(0.01*c1(k)))/k;
c2(k+1)=((((18/1080)*(c1(k)-c2(k)))+((12/108)*((0.1*c3(k))-c2(k)))+((18/108)*(c4(k)-c2(k)))-((1/108)*c2(k))-(0.01*c2(k))))/k;
c3(k+1)=(((1200/18)*(c2(k)-(0.1*c3(k))))-(0.01*c3(k)))/k;
c4(k+1)=(((18/108)*(c2(k)-c4(k)))-((10/108)*c4(k)))/k;
end
Second code i change the initial value and accordingly i got the answer.The second code is
c1(1)=1/120;
c2(1)=0;
c3(1)=0;
c4(1)=0;
for k=1:1:10
c1(k+1)=(((18/120)*(c2(k)-c1(k)))-(0.01*c1(k)))/k;
c2(k+1)=((((18/1080)*(c1(k)-c2(k)))+((12/108)*((0.1*c3(k))-c2(k)))+((18/108)*(c4(k)-c2(k)))-((1/108)*c2(k))-(0.01*c2(k))))/k;
c3(k+1)=(((1200/18)*(c2(k)-(0.1*c3(k))))-(0.01*c3(k)))/k;
c4(k+1)=(((18/108)*(c2(k)-c4(k)))-((10/108)*c4(k)))/k;
end
Correct calue of c1 are
0.008333333 -0.001333333 0.000117083 -7.89E-06 1.14E-06 -1.01E-06 1.12E-06 -1.09E-06 9.24E-07 -6.97E-07 4.73E-07
  3 comentarios
John D'Errico
John D'Errico el 14 de Jun. de 2020
Editada: John D'Errico el 14 de Jun. de 2020
A highly confusing question. Partly so, because the picture you provide of your formulas is itself a picture of confusingly written equations. If someone is to help you to correct your code, then we need to know what are the actual equations.
In some cases, we see expressions where multiplication seems to be implied. That is, we see the relation:
120000(k+1)C_1(k+1) = 1000delta(k) + 18000(C_2(k) - C_1(k)) - 1200C_1
I've used an underscore to indicate what is an apparent subscript. So when no operator is shown, then 120000(k+1), multiplication is implied. Then is C_1(k+1) ALSO an implied multiplication? Or is that now a recursive index? I might guess the latter is what you intend.
But then we see 1000delta(k). Is delta a constant that you have never told us about? It never appears anywhere, in any of the codes you write. Is delta something that you know in advance? Is it something with an index? Is delta a function? In one of your codes, you create the variable B. Does B somehow correspond to delta? In the second code, we see no B at all.
And then later we see C_1(k) in a term. So it would appear that C_1 is a vector that will be grown recursively? But then at the end, we see 1200C_1. Did you just leave out the index there? How should that trailing C_1 be interpreted?
Must I also look at the other equations, wondering what was intended? Wondering what you know but have not explained?
At the end, we see a list of initial values to start out what we are told is a recursion. In there, we see c_1(0) = 0. Then in your first code, we see:
c1(1)=0;
c2(1)=0;
c3(1)=0;
c4(1)=0;
That makes sense, and is consistent with what you have described. But for some reason, in your second code, you arbitrarily change the initial value for C_1.
c1(1)=1/120;
c2(1)=0;
c3(1)=0;
c4(1)=0;
If the starting value is zero, then why did you choose to not do so? And if you do this, then you cannot possibly get the vector c1 which starts at 0, which seems to be your goal.
YOGESHWARI PATEL
YOGESHWARI PATEL el 14 de Jun. de 2020
Sorry for the confusion.Here I am attaching the original equtaion.
Let me clear your confusion delta : delta(k-0)(.Delta is a kroneck delta function) is the original term.Matlab doers not accept zero indexing so my indexing will start from 1 when k=1 delta takes value 1 and for rest value of k it is zero and I sored that value in B in first code and get C_1(2) as 1/120 as C_1(1)=0 is initial value ,C_2(1)=0,C_3(2)=0, C_4(2)=0.As I got mistake in my answer. So in second code I change the initial value and when i draw the graph i add the initial condition and I got the correct answer .
Thank you for your reply hope this will clear your confusion and I will get help in my coding

Iniciar sesión para comentar.

Respuestas (1)

John D'Errico
John D'Errico el 14 de Jun. de 2020
Some of my guesses would have been correct.
Your first equation then has a delta function at k==0. And I'll assume the last term is actually C_1(k).
120000(k+1)C_1(k+1) = 1000delta(k) + 18000(C_2(k) - C_1(k)) - 1200C_1
I'll write the code, as I would want to write it. First, I'll preallocate the vectors. Growing vectors ensures that your code will run slowly. Better yet, is to store all of these vectors in one 2-dimensional array. By doing that, then we can implement the recurrence as an even simpler operation.
I will also point out there are problems in your other equations. For example in the second equation, there is a missing right parenthesis. I guessed where that went, but I may be wrong.
I also did NOT cancel the coefficients you have written, as you could easily make a mistake when doing so.
kmax = 11;
c = zeros(4,kmax);
% write delta as a function, that returns [1;0;0;0] when k==0, and all zeros otherwise.
%
delta = @(k) [double(k==0);0;0;0];
% collect the coefficients of the vectors c into one 4x4 matrix.
M = [[-18000-1200, 18000, 0, 0]; ...
[18000, -18000-120000-180000-10000-10800, 120000*0.1, 180000]; ...
[0, 120000, -120000*0.1-18, 0]; ...
[0, 180000, 0, -180000-100000]];
for kind = 1:kmax-1
% use kind as an offset vector index, recognizing the 1-based index origin of MATLAB
% k is then just kind - 1.
k = kind - 1;
c(:,kind+1) = (1000*delta(k) + M*c(:,kind))./([120000;1080000;1800;1080000]*(k+1));
end
You should see that delta now works as a simple function that will impact only c1, and then only when k==0.
delta(0)
ans =
1
0
0
0
delta(1)
ans =
0
0
0
0
When I run the above code, I find myself not entirely confident this is correct, but that is because I am not confident that your equations were written correctly. I have transcribed them quite carefully as coefficients of the matrix M. In fact, I have checked several times.
c =
Columns 1 through 9
0 0.0083333 -0.00066667 3.9028e-05 -1.9723e-06 2.2721e-07 -1.6865e-07 1.6036e-07 -1.36e-07
0 0 6.9444e-05 -1.0965e-05 5.47e-06 -6.5036e-06 7.3035e-06 -7.0823e-06 6.0119e-06
0 0 0 0.0015432 -0.0027586 0.0037566 -0.0042526 0.0041257 -0.0035022
0 0 0 3.858e-06 -7.0695e-07 2.1899e-07 -1.9012e-07 1.8093e-07 -1.5341e-07
Columns 10 through 11
1.0262e-07 -6.9688e-08
-4.5364e-06 3.0807e-06
0.0026427 -0.0017947
1.1575e-07 -7.8608e-08
The numbers you claim to be "true" appear to be inconsistent with the equations you have written. But that may just mean your equations were still written improperly.

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by