How to call a function, como llamar una funcion

11 views (last 30 days)
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.

Accepted Answer

Jan
Jan on 13 Apr 2021
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)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!