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

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)

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

Helllo, thank you
When I wrote this function line substituting my script name the following occured
clc;
close all;
clear all;
function [y1,...yN] = Attuntitled2.m(x1,...,xM) %function code
t=0:0.01:01
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([0 1 0 1]);
title('Sine Function');
xlabel('Time axis');
ylabel('Amplitude axis')`
end
The command line stated the following
Error: File: Attuntitled2.m Line: 5 Column: 2
Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for equality, use '=='.
How do I fix this?
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.
Yes I have read the documentation and yes i have done a basic Matlab Tutorial. I've taught to write
clc, close all, clear all
at the beginning of all scripts but since thats incorrect i'll remove them and just leave clc.
I'm very new to the software and this is the first assignment I'm doing by myself. How do i change my script so that function can be provided an input as I'm only aware of using inputdlg?
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);
Many thanks; I have followed your exact guide and it seems to still not be working
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.
Here's the updated script
function [y,t] = Attuntitled2.m (ac) %function code
% Generates a sine wave from input parameter (amplitude)
% Returns dataset generated for sine wave in a 0 to 1 second interval
t=0:0.01:01
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
clc;
ac= inputdlg('Enter a value for the amplitude');
a= str2double(ac);
[y,t] = Attuntitled2.m(ac);
and here is what the command line states when the script is ran
Attuntitled2
Error: File: Attuntitled2.m Line: 5 Column: 2
Incorrect use of '=' operator. To assign a value to a variable, use '='. To compare values for equality, use '=='.
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.
I don't understand what you mean by putting the code outside of the function, I was simply following your instructions.
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.

Accedi per commentare.

Categorie

Scopri di più su Loops and Conditional Statements in Centro assistenza e File Exchange

Tag

Commentato:

Rik
il 31 Ago 2020

Community Treasure Hunt

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

Start Hunting!

Translated by