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

3 views (last 30 days)
how to not use all input arguments in the function because some of the arguments are fixed?

Accepted Answer

DGM
DGM on 16 Feb 2022
Edited: DGM on 16 Feb 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 Comments
Alfandias Saurena
Alfandias Saurena on 16 Feb 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

Sign in to comment.

More Answers (1)

Steven Lord
Steven Lord on 16 Feb 2022
Edited: Steven Lord on 16 Feb 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

Categories

Find more on Argument Definitions in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!