MATLAB: Newbie question

variable plot script

Okay, so I've got a solution, which I've successfully turned into a script, so I can run it and change values as I want. But what if I want to make a plot of one of the variables against the solution? I've tried changing the variable in question into 1:10, or x, but then it just doesn't work. I can run it with a different value for the variable a few times and make comments based on that, but it'd be nicer just to have a plot showing how the variable affected the final answer.
Here's my script (I just copied and pasted it from the script editor window): –
f=500
c=340
wavelength=c/f
SB=30
BR=15
SG=2
RG=2
BG=5
pi=3.14159265358979
L_pR=67
L_pA=70
Rvalues=[45 45 45 45 sqrt(45^2+4^2) sqrt(45^2+4^2) sqrt(45^2+4^2) sqrt(45^2+4^2)]
Lvalues=[sqrt(SB^2+(BG-SG)^2)+sqrt(BR^2+(BG-RG)^2) sqrt(SB^2+(BG-SG)^2)+sqrt(BR^2+(BG+RG)^2) sqrt(SB^2+(BG+SG)^2)+sqrt(BR^2+(BG-RG)^2) sqrt(SB^2+(BG+SG)^2)+sqrt(BR^2+(BG+RG)^2) sqrt(SB^2+(BG-SG)^2)+sqrt(BR^2+(BG-RG)^2) sqrt(SB^2+(BG-SG)^2)+sqrt(BR^2+(BG+RG)^2) sqrt(SB^2+(BG+SG)^2)+sqrt(BR^2+(BG-RG)^2) sqrt(SB^2+(BG+SG)^2)+sqrt(BR^2+(BG+RG)^2)]
Nvalues=(2/wavelength)*(Lvalues-Rvalues)
Triangle_biValues=5+20*log10((sqrt(2*pi*Nvalues))./tanh(sqrt(2*pi*Nvalues)))
BeranekSecondHalfvalues=20*log10(Lvalues./Rvalues)
A_biValues=Triangle_biValues+BeranekSecondHalfvalues
A_biNEGdBValues=10.^(-A_biValues/10)
NR_b=10*log10(sum(A_biNEGdBValues))
L_pB=L_pR-((10*log10(10^(-0/10)+10^(-0/10)))-NR_b)
That didn't paste well, so I've put the script file in my public dropbox too – http://dl.dropbox.com/u/11341635/WideBarrierQ1a.m
I want to see how changing BG affects the solution as a plot.
Tom

Best Answer

  • pi is already defined; it is not a good idea to redefine it.
    Remove your BG=5 line.
    Immediately before you calculate Lvalues, add the line
    BGs = [1 2 3 4 5 6 7 8 9 10]; %the list of values you want to use
    L_pB = zeros(1,length(BGs)); %more efficient to preallocate
    for K = 1 : length(BGs)
    BG = BGs(K);
    Then, change your assignment that has L_pB on the left hand side, to instead of L_pB(:,K) on the left hand side. And then add the lines
    end
    plot(BGs, L_pB, 's')
    right after that.
    I have, though, not gone through the code to be sure that you only calculate a single L_pB value per BG value. If instead multiple values are calculated each time, change the 1 in the "zeros" line to be the number of items that are calculated each time (e.g., 7)
    I did not join the points together in the plot because it is not clear to me that the points could be considered ordered or continuous.