Octave and multiple Bode plots - octave

I'm teaching myself Octave and as a motivational exercise am attempting to create some Bode plots. I'd like to create a plot that has multiple curves for different values of a parameter in a transfer function, for example the time constant of a simple RC filter. I'm trying to do it as follows:
tau = [1,2,3]
for i = tau
g(i) = tf(1,[tau(i),1])
endfor
bode(g(1),g(2),g(3))
But it doesn't work, I get the error
error: octave_base_value::imag (): wrong type argument `struct'
However, it works fine if there are not multiple arguments to the bode command and the last line is simply:
bode(g(1))
Any advice as to where I've gone wrong would be appreciated - is there a better way to do what I want to do?

I was able to do it with the following sequence (with octave 3.2.4 on debian):
bode(g(1))
set (findobj (gcf, "type", "axes"), "nextplot", "add")
bode(g(2))
bode(g(3))
The second command is similar to hold on but it works when there are subplots; I found it here.

Using your own code:
subplot(211), hold on
subplot(212), hold on
tau = [1,2,3]
for i = 1:length(tau),
g(i) = tf(1,[tau(i),1]);
bode(g(i))
endfor
The problem with this solution is that you cannot identify a specific plot. You cannot access figure properties through bode() function directly.
Here then a plausible solution to bring you colorful plots:
colorsplot = ["b","m","g"]
tau = [1,2,3]
g = tf(1,[tau(1),1]);
[mag, ph, w] = bode(g);
subplot(211), semilogx(w,20*log(mag)), hold on
subplot(212), semilogx(w,ph), hold on
for i = 2:length(tau),
g = tf(1,[tau(i),1]);
[mag, ph, waux] = bode(g,w);
subplot(211), semilogx(w,20*log(mag),colorsplot(i))
subplot(212), semilogx(w,ph,colorsplot(i))
endfor

Related

Trouble with plotting complex large function in Octave

Hello Iam new with Octave and communications, and I'm trying to simulate different modulations using complex information signal as the following:
But even after I put points before each operator and all solutions that have come to mind I kept getting the same error result:
error: conversion of 71.3086 to octave_idx_type value failed
This is my code:
f = 1e5;
T = 1/f;
t = 0:T/100:2*T;
a = (10^5).*t;
w = 2.*a*pi;
vi = 0.6.+0.6(e.^((-1.*(a.-42).^2)./9).+e.^((-1.*(a.-24).^2)./9).+e.^((-1.*(a.-6).^2)./9)).*sin(w./3).+0.3.*(e.^((-1.*(a.-47).^2)./9).+e.^((-1.*(a.-29).^2)./9).+e.^((-1.*(a.-11).^2)./9)).*cos(w);
plot(t./1e6, vi, 'b', 'LineWidth', 2)
grid on
xlabel('Time (ns)');
ylabel('vi (V)');
xlim([0 (2*T)/f]);
ylim([0 1.2]);
I don't know anymore what to try to plot this.
Anyway thanks for your attention. :)

Plot on higher time frame. Security, mutable variable, global variable, function problem

I'm trying to plot the same shape from the 15 min onto the Daily as well. This is the code to plot a shape on the 15 min which works fine;
if crossover(s3K,s3D) and s3K<25 and (s4K-s4D<3 and s4K-s4D>-3) and s4K<35//or s4D-s4K>0 and s4D-s4K<1 and s4K<50 and s1K<40
rwCross:=true
plotshape(rwCross, style = shape.arrowup, location = location.belowbar, color=color.yellow, size=size.small)
But to plot it on the daily i've tried;
rwCrossDaily = security(syminfo.tickerid,'D', rwCross)
plotshape(rwCrossDaily, style = shape.arrowup, location = location.belowbar, color=color.yellow, size=size.small)
Which gives me the mutable variable error. So i tried using a function to get around it;
rwCross_func() =>
if crossover(s3K,s3D) and s3K<25 and (s4K-s4D<3 and s4K-s4D>-3) and s4K<35//or s4D-s4K>0 and s4D-s4K<1 and s4K<50 and s1K<40
rwCross:=true
rwCrossDaily = security(syminfo.tickerid,'D', rwCross_func())
But now it tells me I 'Cannot modify global variable 'rwCross' in function.'
Help please!
Best solution and cleanest solution here is to just make a bool out of your condition in its simplest form:
rwCross = crossover(s3K,s3D) and s3K<25 and (s4K-s4D<3 and s4K-s4D>-3) and s4K<35//or s4D-s4K>0 and s4D-s4K<1 and s4K<50 and s1K<40
rwCross will naturally become true without the if. This way we do not need to have anything mutable, although there are more solutions for situations that we must...
Cheers!

Trying to define a function that creates lists from files and uses random.choices to choose an element from the weighted lists

I'm trying to define a function that will create lists from multiple text files and print a random element from one of the weighted lists. I've managed to get the function to work with random.choice for a single list.
enter code here
def test_rollitems():
my_commons = open('common.txt')
all_common_lines = my_commons.readlines()
common = []
for i in all_common_lines:
common.append(i)
y = random.choice(common)
print(y)
When I tried adding a second list to the function it wouldn't work and my program just closes when the function is called.
enter code here
def Improved_rollitem():
#create the lists from the files#
my_commons = open('common.txt')
all_common_lines= my_commons.readlines()
common = []
for i in all_common_lines:
common.append(i)
my_uncommons = open('uncommon.txt')
all_uncommon_lines =my_uncommons.readlines()
uncommon =[]
for i in all_uncommon_lines:
uncommon.apend(i)
y = random.choices([common,uncommon], [80,20])
print(y)
Can anyone offer any insight into what I'm doing wrong or missing ?
Nevermind. I figured this out on my own! Was having issues with Geany so I installed Pycharm and was able to work through the issue. Correct code is:
enter code here
def Improved_rollitem():
#create the lists from the files#
my_commons = open('common.txt')
all_common_lines= my_commons.readlines()
common = []
for i in all_common_lines:
common.append(i)
my_uncommons = open('uncommon.txt')
all_uncommon_lines =my_uncommons.readlines()
uncommon =[]
for i in all_uncommon_lines:
uncommon.append(i)
y = random.choices([common,uncommon], [.8,.20])
if y == [common]:
for i in [common]:
print(random.choice(i))
if y == [uncommon]:
for i in [uncommon]:
print(random.choice(i))
If there's a better way to do something like this, it would certainly be cool to know though.

Reading variable length cell arrays into matrix

I am using Octave 4.2 and using xlsread in a for loop to import data from several different RTDs. I am importing using the following code:
for i=rtdmin:rtdmax
filnum=num2str(i);
fid = strcat(pre, filnum, filtyp);
j = exist(fid);
if j == 2
[num{i}, txt{i}, raw{i}, lim{i}] = xlsread(fid);
time{i} = num{i}(:,2);
temp{i} = num{i}(:,3);
endif
endfor
The problem is none of the RTDs have the exact same number of readings (30,000 +-200), or stop and start at the exact same time, although the readings overlap. Because of the variable size of the data in each cell I cannot simply pull it out into a matrix in order to process the data. Can anyone suggest a solution of how to get the data into a matrix, or can suggest a change to the existing code so the data is read into a matrix to begin with. Thank you in advance.

R - Vectorize a JSON call

Working with Mapquest directions API to plot thousands of routes using ggplot2 in R.
Basic code theory: Have a list of end locations and a single start location. For each end location, a call to fromJSON returns routing coordinates from Mapquest. From there, have already vectorized the assignment of coordinates (read as lists in lists) to the geom_path geom of ggplot2.
Right now, running this on a location set of ~ 1200 records takes ~ 4 minutes. Would love to get that down. Any thoughts on how to vectorize the call to fromJSON (which returns a list of lists)?
Windows 7, 64-bit, R 2.14.2
libraries: plyr, ggplot2, rjson, mapproj, XML
k = 0
start_loc = "263+NORTH+CENTER+ST.,+MESA+ARIZ."
end_loc = funder_trunc[,length(funder_trunc)]
route_urls = paste(mapquest_baseurl, "&from=", start_loc, "&to=", end_loc, "&ambiguities=ignore", sep="")
for (n in route_urls) {
route_legs = fromJSON(file = url(n))$route$legs[[1]]$maneuvers
lats = unlist(lapply(route_legs, function(x) return(x$startPoint[[2]])))
lngs = unlist(lapply(route_legs, function(x) return(x$startPoint[[1]])))
frame = data.frame(cbind(lngs, lats))
path_added = geom_path(aes(lngs, lats), data = frame)
p = p + path_added
k = k + 1
print(paste("Processed ", k, " of ", nrow(funder_trunc), " records in set.", sep=""))
}
Going out on a limb here since I don't use rjson or mapproj, but it seems like calling the server thousands of times is the real culprit. If the mapquest server doesn't have an API that allows you to send multiple requests in one go, you are in trouble. If it does, then you need to find out how to use/modify rjson and/or mapproj to call it...
As #Chase said, you might be able to call it in parallel, but the server won't like getting too many parallel requests from the same client - it might ban you. Btw, it might not even like getting thousands of serial requests in rapid succession from the same client either - but apparently your current code works so I guess it doesn't mind.