I've tried to delete some of the extraneous fluff you have in that code, but even there, you have some serious potential problems.
dt=0.07;
M = .1;
g = .01;
Gamma = 0.2;
I = Gamma*M;
tlast = 0.7;
Nt=tlast/dt+1;
Tarray = [0:dt:tlast];
kappa = 1; b=0.6;
h0 = kappa *b*(1-b);
for nt=1:Nt
T = (nt-1)*dt;
X = sym('x', [1,2]);
F = t(X,T,M,g,Gamma);
For example, there is no reason to assign a value to x_1, if two lines later, you will re-define it as symbolic. There is no need to clear variables before you re-assign them to have new values.
Next, things like this will get you into deep trouble, because soon your next anguished question will be why floating point arithmetic does not work properly.
Here tlast was 0.7, and dt was 0.07. While it may indeed happen that 0.7/0.07 is exactly 10, that is not assured in floating point arithmetic. In fact, that result need not even be an integer. So, just for kicks...
It LOOKS like 10. But is it?
Nope.
sprintf('%1.100f',r)
ans =
9.9999999999999982236431605997495353221893310546875000000000000000000000000000000000000000000000000000
The problem is, numbers like 0.7 are not exactly representable in floating point arithmetic. And that number is not exactly 10 times 0.07.
Welcome to the wonderful, wacky world of floating point.
So it is better for you to learn to use tools like linspace. It is always dangerous to do things like
So,for example, what happens if we do this?
0:1:r
ans =
0 1 2 3 4 5 6 7 8 9
BUT r is 10. I think!
Again, r was not truly 10.
Far safer and better is to write it as:
Nt = 11;
Tarray = linspace(0,tlast,Nt);
dt = tlast/Nt;
linspace will always get it right. (In fact, this may be part of the reason why you had troubles.)
Next, I see places where you write this:
int_1 = int(f_11, x_1);
int_2 = int(f_21, x_1);
x_1=1;
upper_1=subs(int_1);
upper_2=subs(int_2);
Using subs like that can be dangerous, because subs might grab all sorts of strange stuff to substitute. After all, I've already seen that you have a tendency to define variables multiple times. You might not realize what you put in there.
Far safer is to use subs like this:
upper_1=subs(int_1,x_1,1);
upper_2=subs(int_2,x_1,1);
This will explicitly force the desired substitution, instead of having subs go searching through your workspace for whatever it might find.
The point is, you have lots of stuff in there that I would need to check if I were you. Does it do exactly what you think it did? What you wanted it to do? When you write code, check every single line. At least do that until you are confident in your coding skills, and even then it is still a good idea.
vpasolve does work correctly, IF you use it correctly.
Best Answer