How to ask for user's input in a program

4 次查看(过去 30 天)
I need to create a program which would ask for user to type any value m and choose between functions (sin or cos). After that I have to calculate an equation x=m:.1:m+4*pi and based on the chosen function the program should draw a discrete data plot. The program should later continue the calculations each 0.5s increasing m by pi/8 for 15 times and then draw a new plot in the earlier figure. I should use for, if, else constructors and I have no idea where to start and how to do it. I would love any kind of help! Thanks in advance!
  4 个评论
Dominykas Repecka
Dominykas Repecka 2018-10-13
prompt='Iveskite skaiciu m: ';
m=input(prompt)
x=m:.1:m+4*pi;
A=sin(x);
B=cos(x);
prompt2='Pasirinkite funkcija cos(B)/sin(A): ';
f=input(prompt2)

请先登录,再进行评论。

采纳的回答

Rik
Rik 2018-10-13
编辑:Rik 2018-10-13
You're already very close. You just need to follow the instructions: use if and else. You should be able to complete the rest of the assignment on your own building on the code below. It uses anonymous functions so you can even expand this very easily to more functions.
prompt='Iveskite skaiciu m: ';
m=input(prompt);
x=m:.1:m+4*pi;
prompt2='Pasirinkite funkcija cos(B)/sin(A): ';
f=input(prompt2,'s');%save as string
if strcmp(f,'A')
fun=@sin;
elseif strcmp(f,'B')
fun=@cos;
else
error('invalid selection')
end
figure(1),clf(1)
plot(x,fun(x))
  4 个评论
Dominykas Repecka
Dominykas Repecka 2018-10-13
Thank you very much! This helped so much, I'll try to do the rest on my own
Dominykas Repecka
Dominykas Repecka 2018-10-20
I've been really struggling understanding the loop function and mainly how it works
% prompt='Iveskite skaiciu m: ';
m=input(prompt);
x=m:.1:m+4*pi;
prompt2='Pasirinkite funkcija cos(B)/sin(A): ';
f=input(prompt2,'s');
if strcmp(f,'A')
fun=@sin;
elseif strcmp(f,'B')
fun=@cos;
else
error('Negalimas pasirinkimas')
end
figure(1),clf(1)
plot(x,fun(x))
% x=m:.1:m+4*pi;
for m = 0:pi/8:15
t = timer('Period', 0.5);
disp(m)
end
how do I figure the last part? I tried to write here something, but i have no idea how the syntax works and help or doc commands dont really help me much

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by