Bisection Method Error - Unable to perform assignment because the indices on the left side are not compatible with the size of the right side.
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Abin Punnilethu Biju
am 27 Okt. 2020
Bearbeitet: Walter Roberson
am 12 Aug. 2023
I'm trying to Run this Below Code with But I am getting this error.Please help me finding the error
tol = 1.e-10;
a = 1.0;
b = 2.0;
nmax = 100;
% Initialization
itcount = 0;
error = 1.0;
% Graph of the function
xval = linspace(a,b,100);
for i=1:100
fval(i) = func(xval(i));
rtns=makereturns(20,2);
end
plot(xval,fval);
grid on;
hold on;
% iteration begins here
while (itcount <= nmax && error >= tol)
itcount = itcount + 1;
% Generate and save iteratres
x = a + (b-a)/2;
z(itcount) = x;
fa = func(a);
fb = func(b);
fx = func(x);
error = abs(fx);
% error = abs(x - xold);
if (error < tol)
x_final = x;
else
if (fa*fx < 0)
% root is between a and x
b = x;
else
% root is between x and b
a = x;
end
end
plot(z(1:itcount),zeros(itcount,1),'r+');
pause(5)
end
if (itcount < nmax);
val = func(x);
fprintf(1,'Converged solution after %5d iterations',itcount);
fprintf(1,' is %15.7e, %e \n',x_final, val);
else fprintf(1,'Not converged after %5d iterations',nmax);
end
function val = func(x)
%val = x^3 + 4 * x^2 - 10;
val = x^3 - x - 3;
%val = sin(x);
end
0 Kommentare
Akzeptierte Antwort
Walter Roberson
am 27 Okt. 2020
rtns=makereturns(20,2);
20 x 2
Q = cov(rtns)
2 x 2
a = [0;1]; B = [1;-1];
2 x 1 each
V = a.*Q*a + 2*a.*Q*B*x + B.*Q*B*x^2
a is 2 x 1, Q is 2 x 2, so a.*Q is going to be 2 x 2 because of implicit expansion. Then you * it by a 2 x 1 and the inner dimensions match for that, so you get back 2 x 1.
Likewise 2*a.*Q*B is 2 x 1 and x is a scalar, so the second term is 2 x 1.
SImilarly B.*Q*B is 2 x 1 and x^2 is scalar so the third term is 2 x 1.
You are adding three 2 x 1 vectors, so V is going to be 2 x 1.
You are now throwing around 2 x 1 vectors and 1 x 2 vectors in the same arithmetic expressions, so you end up getting a 2 x 2 output.
V = a.*Q*a + 2*a.*Q*B*x + B.*Q*B*x^2
Perhaps the expression for that is instead
V = a'*Q*a + 2*a'*Q*B*x + B'*Q*B*x^2
Then that would be 2 x 1 transposed giving 1 x 2, matrix multiply by 2 x 2, giving a 1 x 2 result; matrix multiply by a 2 x 1 getting a 1 x 1 result; likewise for all three terms.
0 Kommentare
Weitere Antworten (1)
Mukter
am 12 Aug. 2023
Bearbeitet: Walter Roberson
am 12 Aug. 2023
tol = 1.e-10;
a = 1.0;
b = 2.0;
nmax = 100;
% Initialization
itcount = 0;
error = 1.0;
% Graph of the function
xval = linspace(a,b,100);
for i=1:100
fval(i) = func(xval(i));
rtns=makereturns(20,2);
end
plot(xval,fval);
grid on;
hold on;
% iteration begins here
while (itcount <= nmax && error >= tol)
itcount = itcount + 1;
% Generate and save iteratres
x = a + (b-a)/2;
z(itcount) = x;
fa = func(a);
fb = func(b);
fx = func(x);
error = abs(fx);
% error = abs(x - xold);
if (error < tol)
x_final = x;
else
if (fa*fx < 0)
% root is between a and x
b = x;
else
% root is between x and b
a = x;
end
end
plot(z(1:itcount),zeros(itcount,1),'r+');
pause(5)
end
if (itcount < nmax);
val = func(x);
fprintf(1,'Converged solution after %5d iterations',itcount);
fprintf(1,' is %15.7e, %e \n',x_final, val);
else fprintf(1,'Not converged after %5d iterations',nmax);
end
function val = func(x)
%val = x^3 + 4 * x^2 - 10;
val = x^3 - x - 3;
%val = sin(x);
end
0 Kommentare
Siehe auch
Kategorien
Mehr zu Colormaps finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!