problems with an interval in a for...end
2 views (last 30 days)
Show older comments
I have difficulties with a assiment. De formula dx/dt=-A*t+B is given. The goal is to Plot x(t) between t=0 and t=tmax. A,B,X(0),tmax and dt should be given by the user.
I just can’t get a proper interval in my for…end. To make matter worst it seems that my supervisor wants me to put another formula in the calculation. Which one has to be an analytical solution (= solved “by hand”) together with a numerical solution.
This are my efforts so far. I tried many things. Which are seen in the program with a %
clear all
close all
%dxdt=-A*x+B
%x(t)=(B*t)/(1+A*t)
A=input('give A: ')
B=input('give B: ')
Xo=input('give X(0): ')
X1=input('give X(1): ')
tmax=input('give t maximaal: ')
dt=input('give interval: ')
K=1:dt:tmax;
for t=1:K
%X(1)=Xo
%X(2)=X1
X(t)=((-A*Xo+B)*exp(-A*(t-1))-B)/(-A)
%X(i+1)=X(i)+(-A*X(i)+B)*dt
%X(t+1)= ((-A*Xo+B)*exp(-A*(t-1))-B)/(-A)+(-A*(((-A*Xo+B)*exp(-A*(t-1))-B)/(-A))+B)*dt
%X=X(t-1)
%T(t)=t
T(length(X))=length(X)
end
%T(length(X))=t+1
plot(T,X,'--r')
0 Comments
Answers (1)
Rick Rosson
on 18 May 2012
t = (0:dt:tmax)';
N = size(t,1);
X = zeros(N,1);
X(1) = Xo;
...
for k = 2:N
...
...
end
figure;
plot(t,X);
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!