Decreasing varible into a loop

조회 수: 13 (최근 30일)
Miguel Angel Villegas Hernandez
Miguel Angel Villegas Hernandez 2021년 7월 20일
댓글: Steven Lord 2021년 7월 20일
Hello
I'm new programming and I have to sove this.
I Have to decrease one varible using i=0 and increasing by 1, i=i+1, but at same time I have another expression that depends of "i" and i need to drecreasing that varible, let you show:
i = 0;
x = b;
Fx = Fb;
Ek = abs(xe-x);
EA = Ek/Ek+1; %Here I've to solbe it
while 1
DeltaX = -Fx/(Fb-Fa)*(b-a);
x = x+DeltaX;
Fx = f(x);
Ek = abs(xe-x);; %Here I've to solbe it
disp ([i a b x Fx DeltaX Ek ]);
if(abs(DeltaX)<Tolerancia && abs(Fx)<Tolerancia)||i >=n
break;
end
a = b;
Fa = Fb;
b = x;
Fb = Fx;
i = i+1;
end
the equation is:
EA = Ek / E_k-1 it must be E_0/E_1 i =0 at the same time k = 2
Thanks
  댓글 수: 4
Miguel Angel Villegas Hernandez
Miguel Angel Villegas Hernandez 2021년 7월 20일
This is my code
f=@(x)x^3-3*x-2
function [Raiz,Iter,Error] = Secante(f,a,b,Tolerancia,IterMax);
xe = -1; %scalar constant
x = a;
Fa = f(a);
x = b;
Fb = f(b);
if abs(Fa) < abs(Fb)
t = a;
a = b;
b = t;
t = Fa;
Fa = Fb;
Fb = t;
end
printf('Calculo de la raiz por el metodo de la secante\n i a b x Fx DeltaX Ek \n');
i = 0;
x = b;
Fx = Fb;
Ek = abs(xe-x); % Ek=abs(-1- x(i))
%here I want to introduce EA=Ek(i)/Ek(k-1),where "k" is another kund of counter linked with "i" counter
% it must be i=0 at same time k=2 to get Ek(0) / Ek(1), Ek(1) / Ek(2)
while 1
DeltaX = -Fx/(Fb-Fa)*(b-a);
x = x+DeltaX;
Fx = f(x);
Ek = abs(xe-x);
disp ([i a b x Fx DeltaX Ek ]);
if(abs(DeltaX)<Tolerancia && abs(Fx)<Tolerancia)||i >=IterMax
break;
end
a = b;
Fa = Fb;
b = x;
Fb = Fx;
i = i+1;
end
Raiz = x;
if abs(DeltaX)<Tolerancia && abs(Fx)<Tolerancia
Error = 0;
else
Error = 1;
end
end
I hope be explicit.
Thanks
Steven Lord
Steven Lord 2021년 7월 20일
There's no element 0 in an array in MATLAB, so E(0) will error if E is a regular numeric array.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by