There are several ways of doing this:
- Use a Live Script (with control slider bar)
- Create a MATLAB function and pass in the parameters you want
- Create a GUI (if using a newer version of MATLAB I would recommend app designer and package as an APP for others)
- Use the Symbolic Toolbox in MATLAB to calculate this (symbolicaly)
Please note I am using the latest version of MATLAB R2018b.
Live Script
Live Script is a MATLAB File that ends in mlx, which is designed for explaining concepts.
mass = 10;
gravity =9.5;
drag_second = 20;
t = 0:.1:10;
rk = mass*gravity - (drag_second/mass) * t;
plot(rk,t)
Please note that you would add the slider after mass, gravity and drag_second.
MATLAB Function
This is what you started to do and gave an error. As you need to pass in the variable, a, b.
function rk= myfunction(a,b);
t = 0:.01:10;
rk= a*b - (c/a)*t;
end
Appdesigner
This is the new way MathWorks has of creating APPS, or User Interfaces.
- Type appdesigner in MATLAB
- Follow the tutorial
- Drag 3 Edit Numeric Fields into your canvas (give them the right name, e.g. mass,gravity,drag_second)
- Drag 1 Axes (for visualizition)
- Drag 1 Push button and call it Run (this is optional but easiest to get you going)
- Select Push button, right click it, Callback, Add PushButtonFcn Callback - this will execute your MATLAB Code when pushed.
- Add the following code:
mass = app.MassEditField.Value;
gravity = app.GravityEditField.Value;
drag_second = app.drag_secondEditField.Value;
t = 0:.01:10;
rk = mass*gravity - (drag_second/mass) * t;
plot(app.UIAxes,rk);
If you are happy with your App, feel free to package it and share it with others.
Hope that this helps you get started.
Best Answer