Syntaxis in the way to write an input for Bisection Method (to find roots of equations)
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Hi, I have an assignment in which I have to find the roots of different equations using the Bisection Method. I wrote a code in Matlab to solve this and I've already been able to solve one correctly, so the code works. The only problem i have is in the syntexis to write my input (which are my equations), the solution for this is probably really simple but Im just really burned out and can't figure it out. I will add the code, an equation as an example and the error it gives me. I hope anyone can help me out :) Please ignore the comments in Spanish, I'm from Mexico

clc
f=input("Introduzca la función colocándola entre comillas sencillas ('): ");
f=inline(f);
a=input("Delimite el límite inferior: ");
b=input("Delimite el límite superior: ");
tol=input("Introduzca el error máximo permitido: ");
if (f(a)*f(b)>0) % Condición del teorema
error("Los signos de las funciones f(a) y f(b) no tiene signos opuestos. El teorema no se cumple"); % Muestra un display alarmante para el usuario y de inmediato detiene el programa
end
cont=0; % El contador nos permite conocer cuantas iteraciones llevamos hasta el momento
while (abs(b-a)>tol) % Esta es la condición correcta para que funcione el programa
m=(a+b)/2; % Esto representa el punto medio
if (f(a)*f(m)<0) % La función cambia de signo en [a,m]
b=m; % Hacemos la notación del cambio de "nombre" del límite inferior
end
if (f(m)*f(b)<0) % La función cambia de signo en [m,b]
a=m; % Hacemos la notación del cambio de "nombre" del límite inferior
end
% Agregamos lo siguiente para asegurar que el programa se detenga si
% el resultado es un valor muy cercano a cero
if (abs(f(m))<eps) % Se usa el épsilon de máquina como la cantidad con la que midamos nuestra condición. Halla la raíz de la ecuación dada.
fprintf("\nLa raíz es %f\n\n",m);
return
end
cont=cont+1;
fprintf("\n Iteración %d, Intervalo [%f,%f]", cont, a, b)
end
fprintf("\n\n")
0 comentarios
Respuestas (1)
Walter Roberson
el 11 de Abr. de 2023
Unless you are specifically required to have compatibility with MATLAB 5.1 or earlier, or unless you are specifically requires by an instructor or manager... do not use inline() . inline() is obsolete and (in nearly all cases) less efficient than the alternatives.
Use input() with the 's' option to get the function from the user, and use str2func to create the function handle.
0 comentarios
Ver también
Categorías
Más información sobre Assembly 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!