How do i create GUI using projectile motion? The GUI is required to be able to input value of x0,y0,v0x,v0y and also able able user to press'redraw plot' button to update figure for projectile motion

2 次查看(过去 30 天)
x0=0;
y0=10;
v0=5;
angle0=(pi/3);
v0x= v0*cos(pi*(angle0/180));
v0y= v0*sin(pi*(angle0/180));
g=9.81;
time = calc_time(g,v0y,y0)
range= calc_range(v0x,time)
v_final=calc_v_final(v0x,v0y)
t=linspace(0,time,100);
x=x0+ v0*cos(angle0)*t;
y=y0+ v0*sin(angle0)*t - g*t.^2/2;
plot(x,y)
xlabel('x-direction displacement');
ylabel('y-direction displacement');
title ('Projectile Motion')

采纳的回答

Geoff Hayes
Geoff Hayes 2015-4-26
Yeap Jia Wei - try using GUIDE to create a GUI that will have four edit text fields (for the x0, y0, v0x, and v0y inputs), a push button for the (re)draw function, and an axes to plot the data. Your above code will need to be converted into a function so that the push button callback will be able to call it and pass the four inputs to it. For example, the function could be
function [x,y] = calcProjectileMotion(x0,y0,v0x,v0y)
g=9.81;
time = calc_time(g,v0y,y0)
range= calc_range(v0x,time)
v_final=calc_v_final(v0x,v0y)
t=linspace(0,time,100);
x=x0+ v0x*t;
y=y0+ v0y*t - g*t.^2/2;
Note how the above returns the projectile motion in the x and y directions which your GUI code (in the push button callback) will plot within the axes.
  2 个评论
Yeap Jia Wei
Yeap Jia Wei 2015-4-29
编辑:Yeap Jia Wei 2015-4-29
I have input these conversion to the respective command, what should i do next? Should i link this 4 values to my push button,'Redraw plot'? How? using createFcn?
function edit8_Callback(hObject, eventdata, handles)
% hObject handle to edit8 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit8 as text
% str2double(get(hObject,'String')) returns contents of edit8 as a double
x0 = str2num(char(get(handles.edit8,'String')));
Geoff Hayes
Geoff Hayes 2015-4-29
Use the pushbutton callback to get the four values from the edit fields and then call your function whose return values you will try to plot.
function pushbutton1_Callback(hObject, eventdata, handles)
x0 = str2num(char(get(handles.edit8,'String')));
y0 = ...;
v0x = ...;
v0y = ...;
[x,y] = calcProjectileMotion(x0,y0,v0x,v0y)
% now plot the data in your axes

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 View and Analyze Simulation Results 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by