How i check to see that the function returns x and y data set in a 0 to 1 second time interal
Mostra commenti meno recenti
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
Risposte (1)
Utkarsh Belwal
il 31 Ago 2020
0 voti
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 Commenti
Estee-Naomie Kadima
il 31 Ago 2020
Rik
il 31 Ago 2020
Have you read the documentation? Have you done a basic Matlab tutorial?
The three dots in normal text means you can fill in intermediate values yourself, in Matlab it means you want to continue the same statement on the next line.
Several issues here:
- You're using clc,close all,clear all. Don't do that unless you understand what they do. With the possible exception of clc, none of these should be used in this case.
- Function can be provided an input. Why aren't you using that instead of inputdlg? Now you can't write a drop-in replacement, which would be one of the major advantages of using functions in the first place: hiding implementation details.
- You can't put code outside of functions if you want to use this as a function file. If you want to use it as a script file you must make sure the function name is different from the file name.
Estee-Naomie Kadima
il 31 Ago 2020
Rik
il 31 Ago 2020
Always including that is a horrible habit. Learn what each of those does and then decide which one to use when:
- clc: only use if you actually want to clear the command window (e.g. if you expect errors or expect to print a lot of text)
- close all: only use if you want to actually close every figure, including any GUIs with useful information that your user may have open at that time. In a debugging situation it is ofter better to guarantee an empty figure by using figure with a number and using clf to clean the figure (e.g. figure(1),clf(1)). Then your actual code can use f=figure; outside of the debugging.
- clear all: only use as part of a global session clearing script. Use functions to separate your workspaces in production, and use clear or clearvars during debugging.
Now to your actual question:
Your function should look something like this:
% what variables should the function return?
% ||| what inputs should this function require?
% ||| ||
function [y,t] = myfun(ac)
% Write a oneliner here that describes the basic idea behind your function.
%
% Write an explanation of your function here.
% Include a description of the required input(s) and what output(s) a user can expect.
% If possible, add a small example to show the syntax in practice
% Only now you should even *start* writing code. By writing the documentation above first
% you force yourself to have a clear goal and explicit assumptions about the inputs.
%I pasted the code you already wrote here
t=0:0.01:01
%because ac should be an input you shouldn't ask a user for it here
f=0.5;
y= a*sin(2*pi*f*t)
plot(t,y);
axis([0 1 0 1]);
title('Sine Function');
xlabel('Time axis');
ylabel('Amplitude axis')
end
Now you have your function. Because it is a function, you can call it the exact same way as you call plot or sin:
clc%
ac= inputdlg('Enter a value for the amplitude');
a= str2double(ac);
[y,t] = myfun(ac);
Estee-Naomie Kadima
il 31 Ago 2020
Rik
il 31 Ago 2020
Then change something.
'it is not working' is not enough information to help you. Remember that I did not hack you computer and I can't look over your shoulder.
Estee-Naomie Kadima
il 31 Ago 2020
Rik
il 31 Ago 2020
Why did you put code outside of the function, but inside the same file? I already told you before you can't mix the two.
Estee-Naomie Kadima
il 31 Ago 2020
Rik
il 31 Ago 2020
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.
Categorie
Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!