Could someone explain why the below code shows an error like "FUNCTION keyword use is invalid"? I have attached the code below.
Show older comments
_% Program to calculate velocity induced by a unit vortex at any point using
% Vector equations of Biot-Savart Law_
V1 = Indvel(-1,0,0,1,0,0,0,5,0);
V2 = Indvel(-1,0,0,1,0,0,20,5,0);
function [Vx Vy Vz] = Indvel(s1,s2,s3,e1,e2,e3,x,y,z)
Gamma = 1; % unit vortex
k = Gamma/(4*pi); % constant
s = [s1 s2 s3];
e = [e1 e2 e3];
r = [x y z];
a = s-r;
b = e-r;
q = (((1 - (dot(a,b)/(norm(a)*norm(b))))*(norm(a)+norm(b)))/(dot(cross(a,b),cross(a,b))))*cross(a,b);
qeff = k.*q;
Vx = qeff(1);
Vy = qeff(2)
Vz = qeff(3);
end
Answers (1)
James Tursa
on 13 Sep 2016
Edited: James Tursa
on 13 Sep 2016
My guess is this is all one script file. You cannot define functions like that in a script file. So split this code into two files. E.g.,
% This is file Indvel_test.m
% Program to calculate velocity induced by a unit vortex at any point using
% Vector equations of Biot-Savart Law_
V1 = Indvel(-1,0,0,1,0,0,0,5,0);
V2 = Indvel(-1,0,0,1,0,0,20,5,0);
and another separate file:
% This is file Indvel.m
function [Vx Vy Vz] = Indvel(s1,s2,s3,e1,e2,e3,x,y,z)
Gamma = 1; % unit vortex
k = Gamma/(4*pi); % constant
s = [s1 s2 s3];
e = [e1 e2 e3];
r = [x y z];
a = s-r;
b = e-r;
q = (((1 - (dot(a,b)/(norm(a)*norm(b))))*(norm(a)+norm(b)))/(dot(cross(a,b),cross(a,b))))*cross(a,b);
qeff = k.*q;
Vx = qeff(1);
Vy = qeff(2);
Vz = qeff(3);
end
Having said that, I will note that the way you have it coded, Indvel returns three separate result variables, while the lines that call Indvel in your script file only get the first one. If you want all three of Vx Vy Vz then you either need to assign all three results, or change Indvel to return a vector of those values. E.g., maybe change the function signature to:
function qeff = Indvel(s1,s2,s3,e1,e2,e3,x,y,z)
2 Comments
hariharan
on 13 Sep 2016
Walter Roberson
on 13 Sep 2016
hariharan, please read http://www.mathworks.com/help/matlab/matlab_prog/function-precedence-order.html
Categories
Find more on Calculus 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!