-1

Here is my code;

%Blasius solution by Euler Method
%---------------
g0=zeros(101,1);
g1=zeros(101,1);
g2=zeros(101,1);
%---------------
g0(1)=0;
g1(1)=0;
% g1(101)=1;
g2(1)=2;
%---------------
G=zeros(101,3);
T=zeros(101,3);
G=[g0 g1 g2];
T=[g1 g2 (-1)*g0.*g2];
%Euler method%
for i=1:100
G(i+1) = G(i) + (T(i)*0.1);
end

What am I missing? I am trying to create the G matrix but it is always a 101*3 zero matrix. It looks like for loop doesn't work but I couldn't figure out why.

Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • You might want to specify whether to take columns or rows in your indices, as opposed to using plain linear ones. Also, your T is always zero, since you multiply zero-elements with zero elements and once with a 2, which is still zero. In your loop you are adding and multiplying zeroes with zeroes, so obviously the result is zero. – Adriaan Oct 18 '15 at 13:22
  • Okey here is the thing; G(1)=[0 0 2] then G(2) =[0 0 2]+[0 2 0]*0.1=[0 0.2 2] G(3)= [0 0.2 2]+(0.2 2 0]*0.1 =[0.2 2.2 2] It suppose to get rid of zeros after third line? – Ozgur Erbulan Oct 18 '15 at 13:34

2 Answers2

1

I figured out why your code is not working:

First of all you need to call row indices, not linear indices, i.e. change your loop to:

for ii=1:100
    G(ii+1,:) = G(ii,:) + (T(ii,:)*0.1);
end

note that I also used ii as opposed to i, since using that as a variable is bad.

This results in T remaining constant, obviously, since you do not change it. You initialise it as a zero array and set only the second element on the first row to 2, but leave the rest as zeros. Adding a row of T to a row of G will therefore not do anything, since you are adding zeros to an existing row. This is why the second row of G becomes [0 0.2 2] and does not change anymore , since you are only adding zeros to it.

You probably forgot to add the line of code which assigns a new value to rows of T. Adhering to your suggestion in the comments:

for ii=1:100
    G(ii+1,:) = G(ii,:) + (T(ii,:)*0.1);
    T(ii+1,:) = G(ii,:);
end
Community
  • 1
  • 1
Adriaan
  • 17,741
  • 7
  • 42
  • 75
  • First of all thank you for your interest. My code is just like that actually i do not forget anything to write in here, as you can see i am a beginner. I have change the for loop but i do not understand how to assing new T and where should i do that. Is it going to be look like that? `G(ii,2)=T(ii,1)` Where should i assing that? Is is going to be in the loop? – Ozgur Erbulan Oct 18 '15 at 14:28
0

Here is the new solution, we have spend hours but finally figure it out.

%Blasius solution by Euler Method
%---------------
g0=zeros(101,1);
g1=zeros(101,1);
g2=zeros(101,1);
g2(1)=2;
%---------------
G=zeros(101,3);
T=zeros(101,3);
G=[g0 g1 g2];
T=[g1 g2 ((-1)*g0.*g2)];
 %Euler method%
for i=1:100
A=[g0(i) g1(i) g2(i)] ;
B=[g1(i)*0.1 g2(i)*0.1 (-1).*g0(i)*g2(i)*0.1];
C(i,:)=A+B;
g0(i+1)=C(i,1);
g1(i+1) =C(i,2);
g2(i+1) =C(i,3);
end