Varying Variables within a Function
Show older comments
The code that I currently have works perfectly for the variables that I have already assigned as constants.
%Setting the time interval in seconds
tspan = [0 20];
%Using the ODE Function
[t,x] = ode45(@Cart,tspan,[5 0]);
%Plotting the graph with a legend
plot(t,x(:,1),'DisplayName','Position'); hold on;
plot(t,x(:,2),'DisplayName','Velocity');
xlabel('Time'); ylabel('X-Values');
legend
%Writing out the function
function output = Cart(t,x)
m=2; %mass
b = 1.3; %damping
k = 20; %spring constant
x1prime = x(2);
x2prime = (-k*x(1)-b*x(2))/m;
output = [x1prime; x2prime];
end
Thank you for your time and help!
3 Comments
Walter Roberson
on 14 Nov 2020
Sam Zavala:
If you feel that your question is unclear, then since you are the author of the question, instead of flagging the question and hoping that a moderator will kindly take the time to figure out what you really meant and re-write the question to be better explained, then you should be spending the time to explain the question better yourself. You are the person who knows best what you need; you should not expect that someone else will spend the time making your question more clear.
Sam Zavala
on 14 Nov 2020
Accepted Answer
More Answers (0)
Categories
Find more on General Applications in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!