how to not use all input arguments in the function because some of the arguments are fixed?

9 visualizaciones (últimos 30 días)
how to not use all input arguments in the function because some of the arguments are fixed?

Respuesta aceptada

DGM
DGM el 16 de Feb. de 2022
Editada: DGM el 16 de Feb. de 2022
If you're writing a function and want certain arguments to be optional (with internal defaults), read about varargin
From the scope of the function, varargin can be handled as a cell array. How you parse/validate its contents is up to your needs.
I generally assign all the parameters to their default values prior to parsing the inputs, overwriting the defaults as the user-defined values are collected from varargin.
  3 comentarios
DGM
DGM el 16 de Feb. de 2022
Unless area() is nested inside another function wherein B is defined, the above definition isn't available to it. It would either need to be explicitly passed to area(), or area() would need to internally define it such that it's a constant or a default for an optionally user-specified parameter.
As I doubt it really makes sense to have a rectangle area function that presumes the size of the rectangle, Steven's suggestion is probably more appropriate.
That said, I'll just offer this for sake of clarification anyway.
area(5,10)
ans = 50
area(5)
ans = 100
function [A] = area(varargin)
% A = area(height,{width})
% calculate the area of a rectangle
% if not specified, the width is assumed to be 20
B = 20; % default
narginchk(1,2);
switch nargin
case 1
h = varargin{1};
case 2
h = varargin{1};
B = varargin{2};
end
A = B*h;
end
Alfandias Saurena
Alfandias Saurena el 16 de Feb. de 2022
So, B is defined inside the function.
this is another function i created. i define (Qn,S,B,M, and ks) for (rough_secant) function because in (secant) function its only need (y) variabel
clc;
clear all;
y=secant(1,@rough_secant);
function [dQ]=rough_secant(y)
Qn=84;
S=0.0005;
B=20;
m=0;
ks=0.006;
A=luas_penampang(B,m,y);
P=keliling_basah(B,m,y);
R=jari2_hidrolis(A,P);
C=chezy_rough(y,ks);
V=kecepatan_aliran(C,R,S);
Qc=V*A;
dQ=Qn-Qc;
end
function [C]=chezy_rough(y,ks)
C=18*log10(12*y/ks);
end
function [us]=shear_velocity(R,S)
us=sqrt(9.81*R*S);
end
function [C]=chezy_smooth(y,vsc,us)
C=18*log10(12*y/(3.3*vsc/us));
end
function [V]=kecepatan_aliran(C,R,S)
V=C*sqrt(R*S);
end
function [A]=luas_penampang(B,m,y)
A=(B+m*y)*y;
end
function [P]=keliling_basah(B,m,y)
P=B+2*sqrt(m^2+1).*y;
end
function [R]=jari2_hidrolis(A,P)
R=A/P;
end
function [y]=secant(x,F_x)
dx=0.01;
y=x;
f=F_x(y);
while abs(f)>0.0000001
f=F_x(y);
f1=F_x(y+dx);
f2=F_x(y-dx);
f3=(f1-f2)/(2*dx);
y2=y-f/f3;
y=y2;
end
end

Iniciar sesión para comentar.

Más respuestas (1)

Steven Lord
Steven Lord el 16 de Feb. de 2022
Editada: Steven Lord el 16 de Feb. de 2022
You can use an anonymous function "adapter".
f = @(in1, in2) max(in1, in2); % I could have used @max
% but I wanted to be explicit
f_2p5 = @(x) f(x, 2.5); % Call f with the first input specified by
% the user and the second fixed by me as 2.5
f_2p5(1:5)
ans = 1×5
2.5000 2.5000 3.0000 4.0000 5.0000
f(1:5, 2.5)
ans = 1×5
2.5000 2.5000 3.0000 4.0000 5.0000

Categorías

Más información sobre Argument Definitions en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by