Varying Variables within a Function
显示 更早的评论
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 个评论
Walter Roberson
2020-11-14
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
2020-11-14
采纳的回答
更多回答(0 个)
类别
在 帮助中心 和 File Exchange 中查找有关 General Applications 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!