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 CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by