How i check to see that the function returns x and y data set in a 0 to 1 second time interal

1 回表示 (過去 30 日間)
Firstly how do i turn the following script into a function?
t=0:0.01:10
ac= inputdlg('Enter a value for the amplitude');
a= str2double(ac);
f=0.5;
y= a*sin(2*pi*f*t)
plot(t,y);
axis([-10 10 -10 10]);
title('Sine Function');
xlabel('Time axis');
ylabel('Amplitude axis')
Secondly how do i check to see that it returns the x and y dataset in a 0 to 1 second time interval?
Many thanks

回答 (1 件)

Utkarsh Belwal
Utkarsh Belwal 2020 年 8 月 31 日
Hi,
In MATLAB, syntax for defining function is as follows:
function [y1,…,yN] = myfun(x1,..,xM)
% function code
end
y1,…,yN are the N outputs of the function and x1,…,xM are the M inputs to the function. Also make sure that the function and script name is same. Refer to the documentation for more information: https://www.mathworks.com/help/matlab/ref/function.html#d120e397526
To get the t and y between 0 and 1 you can define t as,
t = 0:0.01:1;
DISCLAIMER: These are my own views and in no way depict those of MathWorks
  10 件のコメント
Estee-Naomie Kadima
Estee-Naomie Kadima 2020 年 8 月 31 日
I don't understand what you mean by putting the code outside of the function, I was simply following your instructions.
Rik
Rik 2020 年 8 月 31 日
What do you have in your file exactly? Before the 'function' and after the 'end' you should not have anything in that file. You also put in the .m after the function name where you call it. That is not needed.
I would seriously consider doing a less basic Matlab tutorial than you have done. Mathworks provides the Onramp course for free. That should give you a better idea of how to use Matlab. This forum is not very well suited to show you the basics of how a function works.

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

カテゴリ

Help Center および File ExchangeHistorical Contests についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by