How can I store the values for a b mid fa fb fmid at every iteration?
13 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Jorge Arturo Clares Pastrana
le 21 Jan 2022
Commenté : Jorge Arturo Clares Pastrana
le 21 Jan 2022
clear;
clc;
%Use (0, 1) as the initial bracketing interval thus:
a = 0;
b = 1;
%Computing the mid point
mid = (a+b)/2;
%Computing the function f(x) = xe^x - cos(x) at point a,b and mid
format long
fa = (a*exp(a)) - (cos(a));
fb = (b*exp(b)) - (cos(b));
fmid = (mid*exp(mid)) - (cos(mid));
%Code For Loop that identifies the sign and assigns variables accordingly
for k=1:6 %Number of Iterations
if sign(fa) ~= sign(fmid)
b=mid;
else
a=mid;
end
%Compute a b mid fa fb fmid
a;
b;
mid = (a+b)/2;
fa = (a*exp(a)) - (cos(a));
fb = (b*exp(b)) - (cos(b));
fmid = (mid*exp(mid)) - (cos(mid));
end
%Display Last Iteration Values
k
a
b
mid
fa
fb
fmid
0 commentaires
Réponse acceptée
Image Analyst
le 21 Jan 2022
Not sure what you want to do but you definitely need to add indexes to the array so perhaps it's this:
numberOfIterations = 6;
%Use (0, 1) as the initial bracketing interval thus:
a = zeros(1, numberOfIterations);
b = ones(1, numberOfIterations);
%Computing the mid point
mid = (a + b) / 2;
%Computing the function f(x) = xe^x - cos(x) at point a,b and mid
format long
fa = (a .* exp(a)) - (cos(a));
fb = (b .* exp(b)) - (cos(b));
fmid = (mid .* exp(mid)) - (cos(mid));
%Code For Loop that identifies the sign and assigns variables accordingly
for k = 1 : numberOfIterations % Number of Iterations
if sign(fa) ~= sign(fmid)
b(k)=mid(k);
else
a(k)=mid(k);
end
%Compute a b mid fa fb fmid
a(k);
b(k);
mid(k) = (a(k) + b(k))/2;
fa(k) = (a(k) * exp(a(k))) - (cos(a(k)));
fb(k) = (b(k) * exp(b(k))) - (cos(b(k)));
fmid(k) = (mid(k) * exp(mid(k))) - (cos(mid(k)));
end
%Display Last Iteration Values
k
a
b
mid
fa
fb
fmid
4 commentaires
Torsten
le 21 Jan 2022
for k=1:6 %Number of Iterations
if sign(fa) ~= sign(fmid)
b=mid;
else
a=mid;
end
%Compute a b mid fa fb fmid
a;
b;
mid = (a+b)/2;
fa = (a*exp(a)) - (cos(a));
fb = (b*exp(b)) - (cos(b));
fmid = (mid*exp(mid)) - (cos(mid));
K(k) = k;
A(k) = a;
B(k) = b;
MID(k) = mid;
FA(k) = fa;
FB(k) = fb;
FMID(k) = fmid;
end
And initialize the arrays K,A,B,MID,FA,FB,FMID before entering the loop.
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Matrix Indexing dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!