help in matlab

Discussion in 'Programmer's Corner' started by NOW, Dec 24, 2012.

  1. NOW

    Thread Starter New Member

    Dec 21, 2012
    6
    0
    i have a problem here
    in making a project for some transmition line i made code in matlab which is down and the matlabe dos not run it ,why??????:cool::(
    f=0:10e9/201:10e9;
    for t=1:202
    f0=5e9;
    Z0=50;
    w=pi*f(t)/(2*f0);
    a=cos(w);
    b=j*Z0*sin(w);
    c=j*sin(w)/Z0;
    d=cos(w);
    a1=cos(w);
    b1=j*Z0*sin(w)/sqrt(2);
    c1=j*sqrt(2)*sin(w)/Z0;
    d1=cos(w);
    x0=[1 0 0 0 0 0 a1 c1]';
    x1=[0 -1 0 0 0 0 0 0]';
    x2=[(a*Z0)+b (c*Z0)+d -Z0 0 0 0 0 0]';
    x3=[-a*Z0 -c*Z0 Z0 -1 0 0 0 0]';
    x4=[0 0 (a1*Z0)+b1 (c1*Z0)+d1 -Z0 0 0 0]';
    x5=[0 0 0 0 -a*Z0 (Z0*c)+1 -Z0 0]';
    x6=[0 0 a1*Z0 Z0 b-Z0 d 0 0]';
    x7=[0 0 0 0 0 0 0 1]';
    x8=[0 0 0 0 a*Z0 -c*Z0 Z0-b1 -d]';
    y=[x1 x2 x3 x4 x5 x6 x7 x8];
    y1=[x0 x2 x3 x4 x5 x6 x7 x8];
    y2=[x1 x0 x3 x4 x5 x6 x7 x8];
    y3=[x1 x2 x0 x4 x5 x6 x7 x8];
    y4=[x1 x2 x3 x0 x5 x6 x7 x8];
    y5=[x1 x2 x3 x4 x0 x6 x7 x8];
    y6=[x1 x2 x3 x4 x5 x0 x7 x8];
    y7=[x1 x2 x3 x4 x5 x6 x0 x8];
    y8=[x1 x2 x3 x4 x5 x6 x7 x0];
    ia1=det(y1)/det(y);
    ia2=det(y2)/det(y);
    ib1=det(y3)/det(y);
    ib2=det(y4)/det(y);
    ic1=det(y5)/det(y);
    ic2=det(y6)/det(y);
    id1=det(y7)/det(y);
    id2=det(y8)/det(y);
    i1=ia1+id1;
    i2=ia2+ib1;
    i3=ib2+ic2;
    i4=id2-ic1;
    Zin=1/i1;


    G=(Zin-Z0)/(Zin+Z0);

    Gin=sqrt(real(G)^2+imag(G)^2);
    p1=(1-Gin^2)/(2*Z0);
    p2=i2^2*Z0;
    p3=i3^2*Z0;
    p4=i4^2*Z0;

    GdB=10*log(Gin);
    end

    plot(f,GdB);
     
  2. MrChips

    Moderator

    Oct 2, 2009
    12,420
    3,355
    Yeah, I can see why.
     
  3. panic mode

    Senior Member

    Oct 10, 2011
    1,318
    304
    you are going through loop to calculate something for many points, but result is stored in ONE point only. try to replace third last line to

    GdB(t)=10*log(Gin);

    if you are to plot something, it has to be a vector or array
     
  4. NOW

    Thread Starter New Member

    Dec 21, 2012
    6
    0
    sounds that u have it......
     
  5. NOW

    Thread Starter New Member

    Dec 21, 2012
    6
    0
    i dont understand why i have:
    Warning: Divide by zero.
     
  6. panic mode

    Senior Member

    Oct 10, 2011
    1,318
    304
    if you have an error, you need to check what line of code causes it. matlab gives quite detailed feedback. i just run this and it worked for me:

    Code ( (Unknown Language)):
    1. f=0:10e9/201:10e9;
    2.  
    3. f0=5e9; // there is no point to place this inside the loop
    4. Z0=50;
    5.    
    6. for t=1:202
    7.     w=pi*f(t)/(2*f0);
    8.     a=cos(w);
    9.     b=j*Z0*sin(w);
    10.     c=j*sin(w)/Z0;
    11.     d=cos(w);
    12.     a1=cos(w);
    13.     b1=j*Z0*sin(w)/sqrt(2);
    14.     c1=j*sqrt(2)*sin(w)/Z0;
    15.     d1=cos(w);
    16.     x0=[1 0 0 0 0 0 a1 c1]';
    17.     x1=[0 -1 0 0 0 0 0 0]';
    18.     x2=[(a*Z0)+b (c*Z0)+d -Z0 0 0 0 0 0]';
    19.     x3=[-a*Z0 -c*Z0 Z0 -1 0 0 0 0]';
    20.     x4=[0 0 (a1*Z0)+b1 (c1*Z0)+d1 -Z0 0 0 0]';
    21.     x5=[0 0 0 0 -a*Z0 (Z0*c)+1 -Z0 0]';
    22.     x6=[0 0 a1*Z0 Z0 b-Z0 d 0 0]';
    23.     x7=[0 0 0 0 0 0 0 1]';
    24.     x8=[0 0 0 0 a*Z0 -c*Z0 Z0-b1 -d]';
    25.     y=[x1 x2 x3 x4 x5 x6 x7 x8];
    26.     y1=[x0 x2 x3 x4 x5 x6 x7 x8];
    27.     y2=[x1 x0 x3 x4 x5 x6 x7 x8];
    28.     y3=[x1 x2 x0 x4 x5 x6 x7 x8];
    29.     y4=[x1 x2 x3 x0 x5 x6 x7 x8];
    30.     y5=[x1 x2 x3 x4 x0 x6 x7 x8];
    31.     y6=[x1 x2 x3 x4 x5 x0 x7 x8];
    32.     y7=[x1 x2 x3 x4 x5 x6 x0 x8];
    33.     y8=[x1 x2 x3 x4 x5 x6 x7 x0];
    34.     ia1=det(y1)/det(y);
    35.     ia2=det(y2)/det(y);
    36.     ib1=det(y3)/det(y);
    37.     ib2=det(y4)/det(y);
    38.     ic1=det(y5)/det(y);
    39.     ic2=det(y6)/det(y);
    40.     id1=det(y7)/det(y);
    41.     id2=det(y8)/det(y);
    42.     i1=ia1+id1;
    43.     i2=ia2+ib1;
    44.     i3=ib2+ic2;
    45.     i4=id2-ic1;
    46.     Zin=1/i1;
    47.  
    48.  
    49.     G=(Zin-Z0)/(Zin+Z0);
    50.  
    51.     Gin=sqrt(real(G)^2+imag(G)^2);
    52.     p1=(1-Gin^2)/(2*Z0);
    53.     p2=i2^2*Z0;
    54.     p3=i3^2*Z0;
    55.     p4=i4^2*Z0;
    56.  
    57.     GdB(t)=10*log(Gin); // this is the only true change (made GdB an array)
    58. end
    59.  
    60. plot(f,GdB);
     
  7. panic mode

    Senior Member

    Oct 10, 2011
    1,318
    304
    also put this as first line of any matlab script:

    Code ( (Unknown Language)):
    1. clc; clear all; close all;
    so every time you run some code, you get clean sheet. you can use break points to stop code at particular places and narrow down what causes issues, check variables etc.
     
  8. NOW

    Thread Starter New Member

    Dec 21, 2012
    6
    0
    but in the command windows gives me an error....!!
     
  9. panic mode

    Senior Member

    Oct 10, 2011
    1,318
    304
    replace // with % or remove comments.
     
  10. panic mode

    Senior Member

    Oct 10, 2011
    1,318
    304
    Code ( (Unknown Language)):
    1. clc; clear all; close all;
    2.  
    3. f=0:10e9/201:10e9;
    4. f0=5e9;
    5. Z0=50;
    6.    
    7. for t=1:202
    8.     w=pi*f(t)/(2*f0);
    9.     a=cos(w);
    10.     b=j*Z0*sin(w);
    11.     c=j*sin(w)/Z0;
    12.     d=cos(w);
    13.     a1=cos(w);
    14.     b1=j*Z0*sin(w)/sqrt(2);
    15.     c1=j*sqrt(2)*sin(w)/Z0;
    16.     d1=cos(w);
    17.     x0=[1 0 0 0 0 0 a1 c1]';
    18.     x1=[0 -1 0 0 0 0 0 0]';
    19.     x2=[(a*Z0)+b (c*Z0)+d -Z0 0 0 0 0 0]';
    20.     x3=[-a*Z0 -c*Z0 Z0 -1 0 0 0 0]';
    21.     x4=[0 0 (a1*Z0)+b1 (c1*Z0)+d1 -Z0 0 0 0]';
    22.     x5=[0 0 0 0 -a*Z0 (Z0*c)+1 -Z0 0]';
    23.     x6=[0 0 a1*Z0 Z0 b-Z0 d 0 0]';
    24.     x7=[0 0 0 0 0 0 0 1]';
    25.     x8=[0 0 0 0 a*Z0 -c*Z0 Z0-b1 -d]';
    26.     y=[x1 x2 x3 x4 x5 x6 x7 x8];
    27.     y1=[x0 x2 x3 x4 x5 x6 x7 x8];
    28.     y2=[x1 x0 x3 x4 x5 x6 x7 x8];
    29.     y3=[x1 x2 x0 x4 x5 x6 x7 x8];
    30.     y4=[x1 x2 x3 x0 x5 x6 x7 x8];
    31.     y5=[x1 x2 x3 x4 x0 x6 x7 x8];
    32.     y6=[x1 x2 x3 x4 x5 x0 x7 x8];
    33.     y7=[x1 x2 x3 x4 x5 x6 x0 x8];
    34.     y8=[x1 x2 x3 x4 x5 x6 x7 x0];
    35.     ia1=det(y1)/det(y);
    36.     ia2=det(y2)/det(y);
    37.     ib1=det(y3)/det(y);
    38.     ib2=det(y4)/det(y);
    39.     ic1=det(y5)/det(y);
    40.     ic2=det(y6)/det(y);
    41.     id1=det(y7)/det(y);
    42.     id2=det(y8)/det(y);
    43.     i1=ia1+id1;
    44.     i2=ia2+ib1;
    45.     i3=ib2+ic2;
    46.     i4=id2-ic1;
    47.     Zin=1/i1;
    48.  
    49.  
    50.     G=(Zin-Z0)/(Zin+Z0);
    51.  
    52.     Gin=sqrt(real(G)^2+imag(G)^2);
    53.     p1=(1-Gin^2)/(2*Z0);
    54.     p2=i2^2*Z0;
    55.     p3=i3^2*Z0;
    56.     p4=i4^2*Z0;
    57.  
    58.     GdB(t)=10*log(Gin);
    59. end
    60.  
    61. plot(f,GdB);
     
  11. NOW

    Thread Starter New Member

    Dec 21, 2012
    6
    0
    i mean :Warning: Divide by zero.
     
  12. panic mode

    Senior Member

    Oct 10, 2011
    1,318
    304
    i don't know what are you talking about. i just run the code and posted screenshot. and i just repeated it using code in post #10. can you at least tell where you get the warning? what line of code? how are you testing it? are you typing the code in the command window or executing it as a script?

    just open matlab, go to menu File, New, Script and paste code from post #10. then press F5 to run it. if prompted save it anywhere and choose "Change Folder". that's it.
     
  13. NOW

    Thread Starter New Member

    Dec 21, 2012
    6
    0
    this what i have:

    > In YESm at 33
    Warning: Divide by zero.
    > In YESm at 34
    Warning: Divide by zero.
    > In YESm at 35
    Warning: Divide by zero.
    > In YESm at 36
    Warning: Divide by zero.
    > In YESm at 37
    Warning: Divide by zero.
    > In YESm at 38
    Warning: Divide by zero.
    > In YESm at 39
    Warning: Divide by zero.
    > In YESm at 40
    Warning: Imaginary parts of complex X and/or Y arguments ignored.
    > In YESm at 61
    Warning: Divide by zero.
    > In YESm at 33
    Warning: Divide by zero.
    > In YESm at 34
    Warning: Divide by zero.
    > In YESm at 35
    Warning: Divide by zero.
    > In YESm at 36
    Warning: Divide by zero.
    > In YESm at 37
    Warning: Divide by zero.
    > In YESm at 38
    Warning: Divide by zero.
    > In YESm at 39
    Warning: Divide by zero.
    > In YESm at 40
    Warning: Imaginary parts of complex X and/or Y arguments ignored.
    > In YESm at 61
    >>
     
Loading...