Why is my function not working?

조회 수: 18 (최근 30일)
Eric Hofmann
Eric Hofmann 2021년 9월 4일
편집: per isakson 2021년 9월 4일
%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

답변 (3개)

Yongjian Feng
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
Eric Hofmann
Eric Hofmann 2021년 9월 4일
it won't accept the 'end' after the function
Yongjian Feng
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.
  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

댓글을 달려면 로그인하십시오.


per isakson
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)
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 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

카테고리

Help CenterFile Exchange에서 Debugging and Analysis에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by