Trying to solve for roots
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
I am trying to solve for the velocity in this problem. I have attached a photo of my work on paper, but I was trying to set it up in matlab using bisection method, but it calls error when I try to input the large term the includes velocity. I have attached my work on paper and my code. One other thing is my work on paper, in the denominator there should be two parentheses on the right, signifying that the entire denominator is being squared. When I run it, it says my parenthesis are wrong, but I thought I did them correctly. Please help.
% Bisection Method Code #2
max_error=.001;
itr_max=100;
g=9.8
L=80
D=.1
Km=6.5
Re=4826 %coefficient of v in reynolds number
e=.00015
f = @(x) .625-(x^2/(2*g))*(Km+(L/D)*1/(-1.8log((e/(D*3.7))^1.11+6.9/(Re*x)))^2) ; %function
xl=0; %lower bound
xu=100; %upper bound
xr0=0; %initial guess
for i = 1:itr_max
fxl=f(xl);
fxu=f(xu);
xr=(xl+xu)/2;
fxr=f(xr);
err=abs((xr-xr0)/xr)*100;
ferr=abs((fxr-f(xr0))/fxr)*100;
xr0=xr;
fprintf('i=%d \t xl=%f \t xu=%f \t fxl=%f \t fxu=%f \t xr=%f \t fxr=%f \t er=%f\n',i,xl,xu,fxl,fxu,xr,fxr,err)
if err < max_error
break;
end
test1=fxr*fxl;
test2=fxr*fxu;
if test1<0
xu=xr;
elseif test2<0
xl=xr;
end
end
0 comentarios
Respuestas (1)
Cris LaPierre
el 16 de Nov. de 2021
MATLAB does not do implied multiplicaton. You are getting the error in your first image because you have
f = @(x) .625-(x^2/(2*g))*(Km+(L/D)*1/(-1.8log((e/(D*3.7))^1.11+6.9/(Re*x)))^2); %function
% ^^ implied multiplication
Specify the operation to take place between -1.8 and log to fix this error.
f = @(x) .625-(x^2/(2*g))*(Km+(L/D)*1/(-1.8*log((e/(D*3.7))^1.11+6.9/(Re*x)))^2); %function
% ^
Your code runs without error once that change is made. Just be aware that, in MATLAB, log is the natural log, or ln. If you want log base 10, that is log10.
% Bisection Method Code #2
max_error=.001;
itr_max=100;
g=9.8;
L=80;
D=.1;
Km=6.5;
Re=4826; %coefficient of v in reynolds number
e=.00015;
f = @(x) .625-(x^2/(2*g))*(Km+(L/D)*1/(-1.8*log((e/(D*3.7))^1.11+6.9/(Re*x)))^2); %function
xl=0; %lower bound
xu=100; %upper bound
xr0=0; %initial guess
for i = 1:itr_max
fxl=f(xl);
fxu=f(xu);
xr=(xl+xu)/2;
fxr=f(xr);
err=abs((xr-xr0)/xr)*100;
ferr=abs((fxr-f(xr0))/fxr)*100;
xr0=xr;
fprintf('i=%d \t xl=%f \t xu=%f \t fxl=%f \t fxu=%f \t xr=%f \t fxr=%f \t er=%f\n',i,xl,xu,fxl,fxu,xr,fxr,err)
if err < max_error
break;
end
test1=fxr*fxl;
test2=fxr*fxu;
if test1<0
xu=xr;
elseif test2<0
xl=xr;
end
end
Ver también
Categorías
Más información sobre Linear Algebra en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!