How can I call a function with this paramethers
f=-cos(x);
a=-1;
b=1;
n=20;
------
function [cad,g]=biseccion(f,a,b,n)
fa=feval(f,a);
fb=feval(f,b);
cad=' ';
if fa*fb<0
for i=1:n+1
x=(a+b)/2;
fx=feval(f,x);
sal=sprintf('%5d\t%15.8f\t%15.8f\t%15.8f%20s%20s%15s\n',i-1,a,x,b,cadena(fa),cadena(fx),cadena(fa*fx));
if fa*fx<0
b=x;
else
a=x;
fa=fx;
end
cad=strvcat(cad, sal );
end
format long
X=x;
i=n+2;
x=(a+b)/2;
error=abs((x-X)/x)*100;
sal=sprintf('%5d\t%15.8f\t%15.8f\t%15.8f\n',i-1,a,x,b);
cad=strvcat(cad, sal );
g=sprintf('La solución de la ecuación es %10.8f y el error es %1.8f %% \n',X,error);
else
g=sprintf('Cambiar de intervalo\n');
end
end
Im trying to call like this:
biseccion(-cos(x),-1,1,20)
Undefined function or variable 'biseccion'.
>> biseccion(-cos(x),-1,1,20)
Error: File: biseccion.mlx Line: 7 Column: 19
Function with duplicate name "biseccion" cannot be defined.

 채택된 답변

Jan
Jan 2021년 4월 13일

1 개 추천

Insert the code into a file called "bissecion.m" and save it to a folder, which is included in the PATH. See:
doc addpath
Then type in the command window:
f = @(x) -cos(x);
a = -1;
b = 1;
n = 20;
biseccion(f, a, b, n)

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Surfaces, Volumes, and Polygons에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by