Home > other >  I want to creat a matrix in a loop that adds the new data each time to the same matrix
I want to creat a matrix in a loop that adds the new data each time to the same matrix

Time:12-05

my code is this

`

x0=input('Enter first guess (Must be grater than 0): ');
e=input('Enter the desired telorance : ');
n=input(['Enter the desired number of ' ...
    'Iteration (Must be grater than 0): ']);
for Q=[0.0001,0.0002,0.0005,0.001,0.002,0.005,0.01,0.02]
    for Re=[10000,20000,50000,100000,200000,500000,1000000]
        syms f
        t=(1:n);
        eq=(-0.869*log((Q./3.7)   (2.523./(Re*sqrt(f))))).^-2;
        g=@(f) (-0.869*log((Q./3.7)   (2.523./(Re*sqrt(f))))).^-2;
        dg=diff((-0.869*log((Q./3.7)   (2.523./(Re*sqrt(f))))).^-2);
        g0=eval(subs(dg,f,x0));
        if abs(g0)<1
            a=(['The differential of first guess is less than one' ...
                '\n\nabs(g0)= %4.8f\n\n']);
            fprintf(a,abs(g0));
        else
            disp('The differential of first guess is more than one')
            continue
        end
        assume(f,'clear')
        for i=1:n
            x1=g(x0);
            fprintf('x%d = %.8f\n',i,x1)
            x2(1,i)=x1 %this is the part Im having problem with 
            x1=g(x0);
            if abs(x1-x0)<e
                break
            end
            x0=x1;
        end
    end
end

`

I want to add some calculated data to a matrix each time it loops but with out overwriting the old data since I dont know how many numbers Im going to get (it depends on the user input) I need to creat a matrix to put all numbers into it and its going to be a 1 row and (I dont know how many) Columns

when I used this code based on the number of n from the user I get a 1 row and n Column matrix and then It fills by it self until the matrix reach the n Columns then it overwrites the first one


x2 =

0.0198    0.0333    0.0307

x4 = 0.03110199

x2 =

0.0198    0.0333    0.0307    0.0311

The differential of first guess is less than one

abs(g0)= 0.11126141

x1 = 0.02554462

x2 =

0.0255    0.0333    0.0307    0.0311

CodePudding user response:

To fix this issue, you can use the cat function to concatenate the new values onto the existing matrix. For example, you can replace the line

x2(1,i)=x1

with the following:

x2 = cat(2, x2, x1)

This will add the new value to the end of the existing matrix, without overwriting the existing values.

Alternatively, you can pre-allocate the matrix with the desired number of columns, using the zeros function. For example, you can replace the line

x2 = 0

with the following:

x2 = zeros(1, n)

This will create a matrix with one row and n columns, all filled with zeros. Then, when you add new values to the matrix, they will be added to the next empty column, without overwriting any existing values.

You can also use the size function to dynamically determine the size of the matrix, and add new values to the next empty column. For example, you can replace the line

x2(1,i)=x1

with the following:

[numRows, numCols] = size(x2)
x2(1, numCols 1) = x1

This will determine the number of columns in the existing matrix, and add the new value to the next empty column. Note that you will need to initialize the matrix with at least one column, for example using x2 = 0.

  • Related