iteration using for loop is not working

a(1)=0;
b(1)=1;
c(1)=0;
n=1;
for n=1:5
a(2)=0.5*(a(1)-b(1));
b(2)=0.5*((a(1))-(b(1))-(a(1)*c(1)));
c(2)=0.5*((a(1)*b(1)-c(1)));
a(1)=a(2);
b(1)=b(2);
c(1)=c(2);
n=n+1;
end
disp(a(2))
how to run this program to give from old value to new value using iteration a(2) giving more than five value

답변 (1개)

Image Analyst
Image Analyst 2022년 3월 6일

0 개 추천

Try it like this:
numElements = 5;
a = zeros(1, numElements);
b = ones(1, numElements);
c = zeros(1, numElements);
for n = 2 : numElements
a(n) = 0.5*(a(n-1)-b(n-1));
b(n) = 0.5*((a(n-1))-(b(n-1))-(a(n-1)*c(n-1)));
c(n) = 0.5*((a(n-1)*b(n-1)-c(n-1)));
end
a
a = 1×5
0 -0.5000 0 0 0
b
b = 1×5
1.0000 -0.5000 0 0 0
c
c = 1×5
0 0 0.1250 -0.0625 0.0312

댓글 수: 10

shiv gaur
shiv gaur 2022년 3월 6일
why the initial value is not included
Torsten
Torsten 2022년 3월 6일
편집: Torsten 2022년 3월 6일
It is included. a and c are initialized as zeros, b is initialized as ones.
This especially means a(1)=c(1)=0 and b(1)=1.
if this work why this eq is not working
numElements = 20;
a = zeros(1, numElements);
b = ones(1, numElements);
c = zeros(1, numElements);
for n = 2 : numElements
a(n) = (1/n)*10*(a(n-1)-b(n-1));
b(n) = (1/n)*((28*a(n-1))-(b(n-1))-(a(n-1)*c(n-1)));
c(n) = (1/n)*((a(n-1)*b(n-1)-3.3*c(n-1)));
end
a
b
c
1.0e+199 *
Columns 1 through 11
0.0000 -0.0000 -0.0000 -0.0000 -0.0000 0.0000 0.0000 0.0000 -0.0000 -0.0000 -0.0000
Columns 12 through 20
0.0000 0.0000 2.0928 -Inf NaN NaN NaN NaN NaN
c =
1.0e+199 *
Columns 1 through 11
0 0 0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000 -0.0000 0.0000 -0.0000
Columns 12 through 20
-0.0000 0.0000 -3.0100 -Inf Inf NaN NaN NaN NaN
>>
Torsten
Torsten 2022년 3월 6일
It is working, but the recursion gives high values for a, b and c for numElements being a large number.
shiv gaur
shiv gaur 2022년 3월 6일
this is the high value is not possible 10^199
Torsten
Torsten 2022년 3월 6일
Then your recursion is wrong.
shiv gaur
shiv gaur 2022년 3월 6일
this is your last problem you answer but value is not right
Image Analyst
Image Analyst 2022년 3월 6일
We did not evaluate the "correctness" of your formula. I simply used it as is. If your formula is wrong, then correct it. I really have no idea what the "next" value might be based on the prior values, so all I can do is to assume you wrote down the formulas correctly. If you didn't, only you know what the correct formulas should be, not us.
numElements = 20;
a = sym(zeros(1, numElements));
b = sym(ones(1, numElements));
c = sym(zeros(1, numElements));
for n = 2 : numElements
a(n) = (1/sym(n))*10*(a(n-1)-b(n-1));
b(n) = (1/sym(n))*((28*a(n-1))-(b(n-1))-(a(n-1)*c(n-1)));
c(n) = (1/sym(n))*((a(n-1)*b(n-1)-3.3*c(n-1)));
end
a
a = 
b
b = 
c
c = 
digits(10)
vpa(a)
ans = 
vpa(b)
ans = 
vpa(c)
ans = 
numElements = 10;
syms a [1 numElements]
syms b [1 numElements]
syms c [1 numElements]
for n = 2 : numElements
a(n) = (1/sym(n))*10*(a(n-1)-b(n-1));
b(n) = (1/sym(n))*((28*a(n-1))-(b(n-1))-(a(n-1)*c(n-1)));
c(n) = (1/sym(n))*((a(n-1)*b(n-1)-3.3*c(n-1)));
end
Ea = expand(a);
Ca = vpa(findSymType(Ea, 'number'),5);
[~, idx] = sort(abs(Ca), 'desc');
format long g
double(reshape(Ca(idx(1:10)),[],1))
ans = 10×1
1.0e+00 * 2.14539347488294e+24 2.1388215038686e+24 -1.97683468274523e+24 -1.66919049501844e+24 1.57009953308725e+24 -1.53553980415839e+24 1.25698231464319e+24 -1.12800014100977e+24 -1.03005272271465e+24 1.02716449593526e+24
So by the time you get to 10, you are working with coefficients as large as 2*10^24
Ca0 = vpa(findSymType(expand(subs(Ea, [sym('a1'), sym('a3')], [0,0])),'number'),5);
[~, idx] = sort(abs(Ca0), 'desc');
double(reshape(Ca0(1:10), [], 1))
ans = 10×1
1.0e+00 * -9.35288582447091e+18 -6.04096409393064e+18 -1.99711770767288e+18 -1.57887744010591e+18 -1.23085737821287e+18 -3.24385496525373e+17 -2.04394672654123e+17 -5.32546405724897e+16 -4.96159656693842e+16 -4.67790559622922e+16
And after you take into account that a and c start out as 0, you are still working with coefficients in the 10^18 range by the time you get to numElements = 10.
It isn't MATLAB messing up: your formulas are not working out for b as large as 1. If b were less than 1, then you might get very different results.

댓글을 달려면 로그인하십시오.

제품

릴리스

R2021b

태그

질문:

2022년 3월 6일

편집:

2022년 3월 6일

Community Treasure Hunt

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

Start Hunting!

Translated by