필터 지우기
필터 지우기

problems with an interval in a for...end

조회 수: 2 (최근 30일)
Laura
Laura 2012년 5월 18일
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')

답변 (1개)

Rick Rosson
Rick Rosson 2012년 5월 18일
t = (0:dt:tmax)';
N = size(t,1);
X = zeros(N,1);
X(1) = Xo;
...
for k = 2:N
...
...
end
figure;
plot(t,X);

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by