Why is my function not working?
56 ビュー (過去 30 日間)
古いコメントを表示
%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 件のコメント
回答 (3 件)
Yongjian Feng
2021 年 9 月 4 日
You meant:
function U = velocity(n,S,H,B)
U = sqrt(S)*((B*H/(B+2*H))^(2/3))/n;
end
2 件のコメント
Yongjian Feng
2021 年 9 月 4 日
編集済み: Yongjian Feng
2021 年 9 月 4 日
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
2021 年 9 月 4 日
編集済み: per isakson
2021 年 9 月 4 日
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 件のコメント
Image Analyst
2021 年 9 月 4 日
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 件のコメント
参考
カテゴリ
Help Center および File Exchange で Workspace Variables and MAT-Files についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!