Why is my function not working?

18 views (last 30 days)
Eric Hofmann
Eric Hofmann on 4 Sep 2021
Edited: per isakson on 4 Sep 2021
%clear program all together
clear
clc
%create function for velocity(m/s)
function U = (n,S,H,B);
U = sqrt(S)*((B*H/(B+2*H))^(2/3))/n;
T = zeros(6,5);
for i = (1:5)
T(2,1)=0.036;T(2,2)=0.0001;T(2,3)=10;T(2,4)=2;T(2,5)=velocity(.036,.0001,10,2);
T(3,1)=0.020;T(3,2)=0.0002;T(3,3)=8;T(3,4)=1;T(3,5)=velocity(.020,.0002,8,1);
T(4,1)=0.015;T(4,2)=0.0012;T(4,3)=20;T(4,4)=1.5;T(4,5)=velocity(.015,.0012,20,1.5);
T(5,1)=0.030;T(5,2)=0.0007;T(5,3)=25;T(5,4)=3;T(5,5)=velocity(.030,.0007,25,3);
T(6,1)=0.022;T(6,2)=0.0003;T(6,3)=15;T(6,4)=2.6;T(6,5)=velocity(.022,.0003,15,2.6);
end

Answers (3)

Yongjian Feng
Yongjian Feng on 4 Sep 2021
You meant:
function U = velocity(n,S,H,B)
U = sqrt(S)*((B*H/(B+2*H))^(2/3))/n;
end
  2 Comments
Eric Hofmann
Eric Hofmann on 4 Sep 2021
it won't accept the 'end' after the function
Yongjian Feng
Yongjian Feng on 4 Sep 2021
Edited: Yongjian Feng on 4 Sep 2021
Save your function into a velocity.m file, and run the test from matlab command line window.
  1. Your velocity.m has the function:
function U = velocity(n,S,H,B)
U = sqrt(S)*((B*H/(B+2*H))^(2/3))/n;
end
2. Then run the test from the command line window.
T = zeros(6,5);
for i = (1:5)
T(2,1)=0.036;T(2,2)=0.0001;T(2,3)=10;T(2,4)=2;T(2,5)=velocity(.036,.0001,10,2);
T(3,1)=0.020;T(3,2)=0.0002;T(3,3)=8;T(3,4)=1;T(3,5)=velocity(.020,.0002,8,1);
T(4,1)=0.015;T(4,2)=0.0012;T(4,3)=20;T(4,4)=1.5;T(4,5)=velocity(.015,.0012,20,1.5);
T(5,1)=0.030;T(5,2)=0.0007;T(5,3)=25;T(5,4)=3;T(5,5)=velocity(.030,.0007,25,3);
T(6,1)=0.022;T(6,2)=0.0003;T(6,3)=15;T(6,4)=2.6;T(6,5)=velocity(.022,.0003,15,2.6);
end
T

Sign in to comment.


per isakson
per isakson on 4 Sep 2021
Edited: per isakson on 4 Sep 2021
The function must be at the end of the m-file. It's a local file in a script. (Or you can have the script and the function in two separate files. See Scripts vs. Functions.)
%%
T = zeros(6,5);
for i = (1:5)
T(2,1)=0.036;T(2,2)=0.0001;T(2,3)=10;T(2,4)=2;T(2,5)=velocity(.036,.0001,10,2);
T(3,1)=0.020;T(3,2)=0.0002;T(3,3)=8;T(3,4)=1;T(3,5)=velocity(.020,.0002,8,1);
T(4,1)=0.015;T(4,2)=0.0012;T(4,3)=20;T(4,4)=1.5;T(4,5)=velocity(.015,.0012,20,1.5);
T(5,1)=0.030;T(5,2)=0.0007;T(5,3)=25;T(5,4)=3;T(5,5)=velocity(.030,.0007,25,3);
T(6,1)=0.022;T(6,2)=0.0003;T(6,3)=15;T(6,4)=2.6;T(6,5)=velocity(.022,.0003,15,2.6);
end
disp(T)
0 0 0 0 0 0.0360 0.0001 10.0000 2.0000 0.2607 0.0200 0.0002 8.0000 1.0000 0.4278 0.0150 0.0012 20.0000 1.5000 1.8602 0.0300 0.0007 25.0000 3.0000 1.1116 0.0220 0.0003 15.0000 2.6000 0.8872
%% create function for velocity(m/s)
function U = velocity(n,S,H,B);
U = sqrt(S)*((B*H/(B+2*H))^(2/3))/n;
end

Image Analyst
Image Analyst on 4 Sep 2021
Try this:
% Demo by Image Analyst
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 25;
fprintf('Beginning to run %s.m ...\n', mfilename);
% Call the function
U = velocity(4,37,73,42);
fprintf('Done running %s.m\n', mfilename);
T = zeros(6,5);
for i = (1:5)
T(2,1)=0.036;
T(2,2)=0.0001;
T(2,3)=10;
T(2,4)=2;
T(2,5)=velocity(.036,.0001,10,2);
T(3,1)=0.020;
T(3,2)=0.0002;
T(3,3)=8;
T(3,4)=1;
T(3,5)=velocity(.020,.0002,8,1);
T(4,1)=0.015;
T(4,2)=0.0012;
T(4,3)=20;
T(4,4)=1.5;
T(4,5)=velocity(.015,.0012,20,1.5);
T(5,1)=0.030;
T(5,2)=0.0007;
T(5,3)=25;
T(5,4)=3;
T(5,5)=velocity(.030,.0007,25,3);
T(6,1)=0.022;
T(6,2)=0.0003;
T(6,3)=15;
T(6,4)=2.6;
T(6,5)=velocity(.022,.0003,15,2.6);
end
% Create function for velocity(m/s)
function U = velocity(n,S,H,B)
U = sqrt(S)*((B*H/(B+2*H))^(2/3))/n;
end

Categories

Find more on Debugging and Analysis 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!