Hello, i have to plot this signal:
1, -1<=t<=5
f(t)=sin(2*pi*t), 5<t<10
0, 10<t<=15
How can i plot this particular signal and how do i define it?

Réponses (2)

Adam Danz
Adam Danz le 9 Juin 2020
Modifié(e) : Adam Danz le 9 Juin 2020

0 votes

This notation,
x < y < z
is interpretted by Matlab as
(x < y) < z
but since (x < y) is always true (1) or false (0), it can be reduced to
1 < z
% or
0 < z
To create a vector of values between x and z,
x : increment : z
where increment is the step size or use
linspace(x,z,n)
where n is the number of values between and including x and z.
To determine if values in y are between x and z,
x < y & y < z % when working with vectors
x < y && y < z % when working with scalar values

18 commentaires

Eleftherios Venizelos
Eleftherios Venizelos le 10 Juin 2020
So which is the complete code to plot it from start to end?
As Mr.Adam Danz says,
Please create function script file f.m like below
function y = f(t) % when working with vectors t
t( t < -1) = 0; % not actually needed
t(-1 <= t & t <= 5) = 1;
t( 5 < t & t < 10) = sin(2*pi*t(t > 5 & t < 10));
t(10 <= t & t <=15) = 0; % maybe not t>10 but t>=10
t(15 < t ) = 0; % not actually needed
y = t;
end
And then, please put command
t=-1:0.01:15; % To create a vector of values between x and z,
plot(t, f(t));
then you will see expected function.
It is possible to work with scalar value t by calling f(t) many time.
function y = f(t) % when working with scalar values t
if (t < -1)
y = 0; % not actually needed
elseif(-1 <= t && t <= 5)
y = 1;
elseif( 5 < t && t < 10)
y = sin(2*pi*t);
elseif(10 <= t && t <=15) % maybe not t>10 but t>=10
y = 0;
else
y = 0; % not actually needed
end
end
Adam Danz
Adam Danz le 10 Juin 2020
Given the information from my answer, what changes do you think should be made?
There are multiple interpretations of the code in your question. It looks like Atsushi Ueno's comments may be helpful to you but it's up to you to decide what the correct interpretation is.
Eleftherios Venizelos
Eleftherios Venizelos le 10 Juin 2020
It gives me an error in line 2:
Not enough input arguments.
Adam Danz
Adam Danz le 10 Juin 2020
What's line 2?
Share your code and share the entire error message.
Eleftherios Venizelos
Eleftherios Venizelos le 10 Juin 2020
Adam Danz
Adam Danz le 10 Juin 2020
Modifié(e) : Adam Danz le 10 Juin 2020
Look at the first line of your code, specifically the part that's underlined in red.
Also, in the future, share copy-pasted code as test rather than images. We can't copy an image and run it in Matlab.
And again, always share the full error message.
Eleftherios Venizelos
Eleftherios Venizelos le 10 Juin 2020
Modifié(e) : Adam Danz le 11 Juin 2020
the function is:
function y = f(t) % when working with vectors t
t( t < -1) = 0; % not actually needed
t(-1 <= t & t <= 5) = 1;
t( 5 < t & t < 10) = sin(2*pi*t(t > 5 & t < 10));
t(10 <= t & t <=15) = 0; % maybe not t>10 but t>=10
t(15 < t ) = 0; % not actually needed
y = t;
end
the main program:
t=-1:0.01:15; % To create a vector of values between x and z,
plot(t, f(t));
and the error i get:
Undefined function 'f' for input arguments of type 'double'.
Error in askisi6 (line 3)
plot(t, f(t));
Please put 'path' into your MATLAB context. MATLAB has its search path list like OS.
If the file f.m which f() is defined in, is not included in any of the path list, you have to add your folder's path into the MATLAB search path list
example:
oldpath = path;
path(oldpath,'c:\tools\goodstuff')
@Eleftherios Venizelos , if that error message was shared from the start, you could have had an answer within seconds. That error message tells us what the problem is.
As Atsushi Ueno suggested, add the directory that stores the file containing f() to your path using
addpath('C:\Users\name\Documents\MATLAB\.................')
% Insert your path
Eleftherios Venizelos
Eleftherios Venizelos le 11 Juin 2020
If i got that right because i have never used it before,i have to write this command in my main program so as to give the path of my function file?For instance:
In main program:
addpath('E:\Users\efthy\Desktop\y').
y is the name of my function file.
Adam Danz
Adam Danz le 11 Juin 2020
Yes. Alternatively, you could add it to your startup.m file if you have one. This file is executed when you start matlab.
Eleftherios Venizelos
Eleftherios Venizelos le 11 Juin 2020
When i run the function file it gives me:
Error using y (line 2)
Not enough input arguments.
when i run the main file:
Error: File: askisi6_teliko.m Line: 2 Column: 85
Expression or statement is incomplete or incorrect
it detects an error in addpath command:the code is:
t=-1:0.01:15;
addpath('E:\Users\efthy\Desktop\y').% To create a vector of values between x and z,
plot(t, f(t));
Adam Danz
Adam Danz le 11 Juin 2020
Sorry, I answered too quickly in my last comment.
You should not include the filename. You're adding the path to the directory that stores the file.
Look at the documentation for addpath() and path().
Eleftherios Venizelos
Eleftherios Venizelos le 11 Juin 2020
i fixed that but now it gives me this:
Undefined function 'f' for input arguments of type 'double'.
Error in askisi6_teliko (line 3)
plot(t, f(t));
The function name must match the file name. Secondly, the function name 'f' is a horrible function name. Atsushi Ueno may have given that as a generic example but you should replace that name with a meaninful function name. Good function names often include a verb that describes what the function does.
writetable()
readtable()
addpath()
plot()
get()
These all contain verbs.
Atsushi Ueno
Atsushi Ueno le 12 Juin 2020
I'm sorry I've suddenly gotten into this conversation and wrote unclear comment.
But now I have learned how to answer appropriately through this conversation.
Thank you so much Eleftherios Venizelos and Adam Danz.

Connectez-vous pour commenter.

Eleftherios Venizelos
Eleftherios Venizelos le 11 Juin 2020

0 votes

I dont thinks this is the problem.I have wrote many main programms with functions.Now it gives me this:
function:
function y = f(t) % when working with vectors t
t( t < -1) = 0; % not actually needed
t(-1 <= t & t <= 5) = 1;
t( 5 < t & t < 10) = sin(2*pi*t(t > 5 & t < 10));
t(10 <= t & t <=15) = 0; % maybe not t>10 but t>=10
t(15 < t ) = 0; % not actually needed
y = t;
end
main:
t=-1:0.01:15; % To create a vector of values between x and z,
plot(t, f(t));
error:
Undefined function 'f' for input arguments of type 'double'.
Error in askisi6m (line 2)
plot(t, f(t));

2 commentaires

The function name must match the file name.
Secondly, the function name 'f' is a horrible function name. Atsushi Ueno may have given that as a generic example but you should replace that name with a meaninful function name. Good function names often include a verb that describes what the function does.
writetable()
readtable()
addpath()
plot()
get()
These all contain verbs.
Eleftherios Venizelos
Eleftherios Venizelos le 11 Juin 2020
Ok stupid error.Now works fine.Thanks for everything!

Connectez-vous pour commenter.

Catégories

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by