How to call a function, como llamar una funcion

19 ビュー (過去 30 日間)
Ana Carla Díaz Aguirre
Ana Carla Díaz Aguirre 2021 年 4 月 13 日
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 日
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 件)

カテゴリ

Help Center および File ExchangeCalculus についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by