How to call a function

1 回表示 (過去 30 日間)
Ryan Hyatt
Ryan Hyatt 2020 年 3 月 25 日
コメント済み: Ryan Hyatt 2020 年 3 月 25 日
I have an assignment that asks for one function that uses user input to compute outputs to different questions. my code follows,
clear,clc,close all
function [C1, C2, C3, C4, C5, s, m] = Spring(f, i)
% start of homework #3 function
%access question #1 subfunction
m = convert(f);
%access question #2 subfunction
s = sum(i);
%code for question #3
%% Question 3
d = 0:0.25:3;
h = 1;
C1 = 0.5.*d.^3-(1.5*h.*d.^2)+(1+h.^2).*d;
plot(d,C1)
hold on
h = 1.5;
C2 = 0.5.*d.^3-(1.5*h.*d.^2)+(1+h.^2).*d;
plot(d,C2)
hold on
h = 2;
C3 = 0.5.*d.^3-(1.5*h.*d.^2)+(1+h.^2).*d;
plot(d,C3)
hold on
h = 2.5;
C4 = 0.5.*d.^3-(1.5*h.*d.^2)+(1+h.^2).*d;
plot(d,C4)
hold on
h = 3;
C5 = 0.5.*d.^3-(1.5*h.*d.^2)+(1+h.^2).*d;
plot(d,C5)
xlabel('X');
ylabel('Y');
end
%% Question 1 subfunction
function m = convert(f)
m = f/3.28084;
end
%% Question 2 subfunction
function s = sum(i)
while i<11
s = sum(factorial(i));
end
end
I have tried both running the function by clicking the run button and calling the function by "Spring" in the command window however i get this Error Message: Cannot find an exact (case-sensitive) match for
'Spring'
Please excuse me as my knowledge is limited in this subject

採用された回答

Stephen23
Stephen23 2020 年 3 月 25 日
編集済み: Stephen23 2020 年 3 月 25 日
Make these changes:
clear,clc,close all % <-------- DELETE THIS AWFUL ANTIPATTERN LINE OF EVIL
function [C1, C2, C3, C4, C5, s, m] = Spring(f, i) % <--- THIS MUST BE THE FIRST LINE ON THE FILE
Beginners overuse CLEAR,CLC,CLOSE in code. Don't use them if they are not required (hint: almost never).
Beginners overuse that attractive green RUN button. It is rarely worth the trouble using. Just call the function from the command window (or whatever script, etc.). How to call functions is explained in the introductory tutorials:
You need to make sure that your code is saved in a file named the same as the first function in the file, i.e.:
Spring.m
For MATLAB to see it, the file needs to be saved on the MATLAB Search Path or in the current directory.
Then call your function from the command line like this:
f = whatever_f_is_supposed_to_be;
i = whatever_i_is_supposed_to_be;
[out1,out2,out3,out4,out5,outS,outM] = Spring(f, i)
  1 件のコメント
Ryan Hyatt
Ryan Hyatt 2020 年 3 月 25 日
Thank you very much!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by