Question

Here is my code:

%Initial Conditions:

y(1)=1;
Dy(1)=0;
DDy(1)=0;
t(1)=0;

%Parameters:
dt = 0.1;
nsteps =10/dt;

for i=1:nsteps
    t(i+1) = t(i) + dt;
    DDy(i+1) = 12*t.*y(i) + (18*t.^2 - 6).*(y(i).^2).*Dy(i);
    Dy(i+1) = Dy(i) + DDy(i)*dt;
    y(i+1) = y(i) + Dy(i+1)*dt;
end

And here is the error message:

In an assignment A(I) = B, the number of elements in B and I must be the same.

Error in euler_croemer_de (line 20) DDy(i+1) = 12*t.*y(i) + (18*t.^2 - 6).*(y(i).^2).*Dy(i);


I have tried searching on the internet, that I might resolve the issue myself. However, I honestly couldn't find anything helpful.

I would appreciate someones help.

Was it helpful?

Solution

In the line

DDy(i+1) = 12*t.*y(i) + (18*t.^2 - 6).*(y(i).^2).*Dy(i);

t contains i+1 elements, so the right-hand side is a vector of length i+1. However, the left-hand side refers to a single value, namely one entry of DDy. So you are trying to assign a vector to a single entry of DDy. Hence the error.

Maybe you mean t(i+1) instead of t on that line?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top