Unable to use function in script
9 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Manish
el 27 de En. de 2019
I am new to matlab, i created a function file and it is working fine while using in command winow but showing "Not enough input arguments." while using in a script
function [V,A,Up,dV,dA] = VAUt(ct,B,L,r,R,Ach,Ap,N)
B = B*0.0254;
L = L*0.0254;
Ach = Ach*0.00064516;
Ap = Ap*0.00064516;
Vd = 22*(B^2)*L/28;
Vc = Vd/(r-1);
V = Vc*(1+0.5*(r-1)*(R+(1- cosd(ct))-((R^2)-((sind(ct))^2))^0.5));
A = Ach + Ap + 22*B*L*(R+1-cosd(ct)+((R^2)-(sind(ct))^2))/14;
Up = 22*L*N*sind(ct)*(cosd(ct)/(((R^2)-(sind(ct))^2)^0.5)+1)/7;
dV = Vc*(R/2 - cos((pi*ct)/180)/2 - (R^2 - sin((pi*ct)/180)^2)^(1/2)/2 + 1/2);
dA = (11*B*L*((pi*sin((pi*ct)/180))/180 - (pi*cos((pi*ct)/180)*sin((pi*ct)/180))/90))/7;
end
in = [15,14,14,9,6,231,154,1500];
[Vt,At,Upt,dVt,dAt] = VAUt(in);
A = [Vt,At,Upt,dVt,dAt];
for i = 0:1:36
in(1) = i*5;
A(:,:,i) = VAUt(in);
end
0 comentarios
Respuesta aceptada
Stephan
el 27 de En. de 2019
Editada: Stephan
el 27 de En. de 2019
Hi,
you define an array as input arguments. Matlab interprets your array as one input argument, not as 8 as you think. This is because Matlab accepts vectorized functions, which would accept vectors of every single input argument and would calculate the function for all of them with as many function results as the vector has elements. This saves time and no for loops are needed. But the other side is, that you can not give multiple input arguments the way you do.
Best regards
Stephan
2 comentarios
Stephan
el 27 de En. de 2019
Editada: Stephan
el 27 de En. de 2019
ct = 15;
B = 14;
L = 14;
...
[Vt,At,Utp,...] = VAUt(ct,B,L,...)
or you rewrite your function to accept the vector argument for scalar values of all inputs:
function [V,A,Up,dV,dA] = VAUt(in)
% Give values to function
ct = in(1);
B = in(2);
L = in(3);
r = in(4);
R = in(5);
Ach = in(6);
Ap = in(7);
N = in(8);
% calculate results
B = B*0.0254;
L = L*0.0254;
Ach = Ach*0.00064516;
Ap = Ap*0.00064516;
Vd = 22*(B^2)*L/28;
Vc = Vd/(r-1);
V = Vc*(1+0.5*(r-1)*(R+(1- cosd(ct))-((R^2)-((sind(ct))^2))^0.5));
A = Ach + Ap + 22*B*L*(R+1-cosd(ct)+((R^2)-(sind(ct))^2))/14;
Up = 22*L*N*sind(ct)*(cosd(ct)/(((R^2)-(sind(ct))^2)^0.5)+1)/7;
dV = Vc*(R/2 - cos((pi*ct)/180)/2 - (R^2 - sin((pi*ct)/180)^2)^(1/2)/2 + 1/2);
dA = (11*B*L*((pi*sin((pi*ct)/180))/180 - (pi*cos((pi*ct)/180)*sin((pi*ct)/180))/90))/7;
end
Then you can calculate the function in the given form:
A = zeros(1,5,36);
in = [5,14,14,9,6,231,154,1500];
for i = 1:36
in(1) = i*5;
[Vt,At,Upt,dVt,dAt] = VAUt(in);
A(:,:,i) = [Vt,At,Upt,dVt,dAt];
end
Note that i corrected two additional error in your for loop, due to i=0 is not valid and your code leaded to some curios results for A.
Más respuestas (0)
Ver también
Categorías
Más información sobre Tracking and Motion Estimation en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!