Could someone explain why the below code shows an error like "FUNCTION keyword use is invalid"? I have attached the code below.

_% 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)

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)

Asked:

on 13 Sep 2016

Commented:

on 13 Sep 2016

Community Treasure Hunt

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

Start Hunting!