Array indices must be positive integers or logical values.

1 visualizzazione (ultimi 30 giorni)
x = 0:0.1:4;
f = x;
g = -2*log10(0.01+10e-4*x);
plot(x,f,x,g);
func = 2*log10(0.01+10e-4*x) + x;
res = bisect(func, 0, 4, 10e-8)
function xc = bisect(f,a,b,tol)
if sign(f(a))*sign(f(b)) >= 0
error('f(a)f(b)<0 not satisfied!') % ceases execution
end
fa = f(a);
fb = f(b);
while (b-a)/2 > tol
c = (a+b)/2;
fc = f(c);
if fc == 0 % c is a solution, done
break
end
if sign(fc)*sign(fa) < 0 % a and c make the new interval
b = c; fb = fc;
else % c and b make the new interval
a = c; fa = fc;
end
end
xc = (a+b)/2; % new midpoint is best estimate
end

Risposte (1)

VBBV
VBBV il 4 Giu 2023
x = 0:0.1:4;
f = x;
g = -2*log10(0.01+10e-4*x);
plot(x,f,x,g);
func = @(x) 2*log10(0.01+10e-4*x) + x;
res = bisect(func, 0, 4, 10e-8)
res = 3.7250
function xc = bisect(func,a,b,tol)
if sign(func(a))*sign(func(b)) >= 0
error('f(a)f(b)<0 not satisfied!') % ceases execution
end
fa = func(a);
fb = func(b);
while (b-a)/2 > tol
c = (a+b)/2;
fc = func(c);
if fc == 0 % c is a solution, done
break
end
if sign(fc)*sign(fa) < 0 % a and c make the new interval
b = c; fb = fc;
else % c and b make the new interval
a = c; fa = fc;
end
end
xc = (a+b)/2; % new midpoint is best estimate
end
  1 Commento
VBBV
VBBV il 4 Giu 2023
Modificato: VBBV il 4 Giu 2023
The below line is probably the anonymous function which you wanr to evaluate using bisect function
func = @(x) 2*log10(0.01+10e-4*x) + x;

Accedi per commentare.

Categorie

Scopri di più su Modeling in Help Center e File Exchange

Tag

Community Treasure Hunt

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

Start Hunting!

Translated by