Why is my function not working?
23 views (last 30 days)
Show older comments
%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
0 Comments
Answers (3)
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
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.
- 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
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)
%% 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
0 Comments
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
0 Comments
See Also
Categories
Find more on Standard File Formats 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!