horizontal line next to specif bar to indicates the half of that bar - bar-chart

Good morning everyone,
I cannot create a code that, upon the occurrence of the first condition, which colors the bar green or red, draws a horizontal line next to this bar that indicates the precise half of that bar.
Thanks to those who want to help me
This is my code, but this is my code, but unfortunately it doesn't print any lines:
//#version=5
indicator("P_O", overlay=true)
leftBars = 4 //left strenght
rightBars = 0 //right strenght
swh = ta.pivothigh(leftBars, rightBars)
swl = ta.pivotlow(leftBars, rightBars)
swh_cond = not na(swh)
swl_cond = not na(swl)
//--------------------------------------------Identifica High & Low------------------------------------------------------------------
lBars = 4 //left strenght
rBars = 4 //right strenght
awh = ta.pivothigh(lBars, rBars)
awl = ta.pivotlow(lBars, rBars)
awh_cond = not na(awh)
awl_cond = not na(awl)
//-----------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------Signal_Green_Long----------------------------------------------------------------------
barcolor(close > open and swl_cond and ((close - low) / (open - low)) > (75/100) and ((close - low) / (open - low)) < (300/100) and ((high - close) / (open - low)) < (50/100) ? #008000 : na)
//Stampa avviso segnale
Signal_Green_Long = (close > open and swl_cond and ((close - low) / (open - low)) > (75/100) and ((close - low) / (open - low)) < (300/100) and ((high - close) / (open - low)) < (50/100))
plotshape (Signal_Green_Long, text='Segnale Long',color=#008000)
//-----------------------------------------------------------------------------------------------------------------------------------
//--------------------------------------------stampa 1/2 candela---------------------------------------------------------------------
half= ((high-low)/2)
t1 = ta.valuewhen(Signal_Green_Long,(high-low)/2,0)
plot(t1, color=t1 != t1[1] ? na : color.black)
//-----------------------------------------------------------------------------------------------------------------------------------

Related

Can't save data using push button (MATLAB)

I'm trying to create a figure where the user can select cells to turn on or off. Then, the user can click a button 'Enter' to save the board as an array. I successfully found a way to create interactivity in my plot thanks to a very useful explanation I found here. I just made some changes to suit my needs.
However, I can't find a way to save the board. The button is working (or at least isn't not working), but the data isn't saved. And I don't know how to fix that. Any help would be appreciated.
Here is my code:
function CreatePattern
hFigure = figure;
hAxes = axes;
axis equal;
axis off;
hold on;
% create a button to calculate the difference between 2 points
h = uicontrol('Position',[215 5 150 30],'String','Enter','Callback', #SaveArray);
function SaveArray(ButtonH, eventdata)
global initial
initial = Board;
close(hFigure)
end
N = 1; % for line width
M = 20; % board size
squareEdgeSize = 1;
% create the board of patch objects
hPatchObjects = zeros(M,M);
for j = M:-1:1
for k = 1:M
hPatchObjects(M - j+ 1, k) = rectangle('Position', [k*squareEdgeSize,j*squareEdgeSize,squareEdgeSize,squareEdgeSize], 'FaceColor', [0 0 0],...
'EdgeColor', 'w', 'LineWidth', N, 'HitTest', 'on', 'ButtonDownFcn', {#OnPatchPressedCallback, M - j+ 1, k});
end
end
%global Board
Board = zeros(M,M);
playerColours = [1 1 1; 0 0 0];
xlim([squareEdgeSize M*squareEdgeSize]);
ylim([squareEdgeSize M*squareEdgeSize]);
function OnPatchPressedCallback(hObject, eventdata, rowIndex, colIndex)
% change FaceColor to player colour
value = Board(rowIndex,colIndex);
if value == 1
set(hObject, 'FaceColor', playerColours(2, :));
Board(rowIndex,colIndex) = 0; % update board
else
set(hObject, 'FaceColor', playerColours(1, :));
Board(rowIndex,colIndex) = 1; % update board
end
end
end
%imwrite(~pattern,'custom_pattern.jpeg')

Double pendulum animation in python

I am using the following code to solve the system of differential equations numerically and then animate it:
for i in range(steps-1):
Theta1 = t1[i]
Theta2 = t2[i]
dTheta1 = w1[i]
dTheta2 = w2[i]
a1 = (g*(np.sin(Theta2)*np.cos(Theta1-Theta2)-mu*np.sin(Theta1))-(l2*dTheta2*dTheta2+l1*dTheta1*dTheta1*np.cos(Theta1-Theta2))*np.sin(Theta1-Theta2))/(l1*(mu-np.cos(Theta1-Theta2)*np.cos(Theta1-Theta2)))
a2 = (mu*g*(np.sin(Theta1)*np.cos(Theta1-Theta2)-np.sin(Theta2))+(mu*l1*dTheta1*dTheta1+l2*dTheta2*dTheta2*np.cos(Theta1-Theta2))*np.sin(Theta1-Theta2))/(l2*(mu-np.cos(Theta1-Theta2)*np.cos(Theta1-Theta2)))
w1[i+1] = w1[i] + dt*a1
w2[i+1] = w2[i] + dt*a2
t1[i+1] = t1[i] + dt*w1[i]
t2[i+1] = t2[i] + dt*w2[i]
This gives me errors such as
RuntimeWarning: invalid value encountered in longdouble_scalars
and
RuntimeWarning: overflow encountered in longdouble_scalars.
At first, I thought this might be happening due to precisions errors, so I tried using longdouble. But that didn't help at all. When I run the animation, I get a few frames of unphysical systems and then it shuts downs. I have checked my equation from multiple sources and I ended up using their equations too and the problem still persists. What am I doing wrong?
Edit: Okay so simply decreasing the step size seemed to not give me the error any more, but now animation is unphysical. The pendulum seems to pick up speed over time, and also performs full rotations, even when it started from rest at an extreme. I am using a 4th order RK method and still getting this problem. Any ideas? I have provided my new code below
def diff_eq_a1(theta1,theta2,w1,w2):
a11 = mu
a12 = np.cos(theta1-theta2)
a21 = np.cos(theta1-theta2)
a22 = 1
b1 = (mu*g*l1*np.sin(theta1)) + (l2*np.sin(theta1-theta2)*w2*w2)
b2 = (g*l2*np.sin(theta2))-(l1*np.sin(theta1-theta2)*w1*w1)
return -(1/l1)*(b1*a22-b2*a12)/(a11*a22-a21*a12)
def diff_eq_a2(theta1,theta2,w1,w2):
a11 = mu
a12 = np.cos(theta1-theta2)
a21 = np.cos(theta1-theta2)
a22 = 1
b1 = (mu*g*l1*np.sin(theta1)) + (l2*np.sin(theta1-theta2)*w2*w2)
b2 = (g*l2*np.sin(theta2))-(l1*np.sin(theta1-theta2)*w1*w1)
return -(1/l2)*(b2*a11-b1*a21)/(a11*a22-a21*a12)
for i in range(steps-1):
a1_k1 = dt*diff_eq_a1(t1[i],t2[i],w1[i],w2[i])
a1_k2 = dt*diff_eq_a1((t1[i]+0.5*a1_k1),t2[i],w1[i],w2[i])
a1_k3 = dt*diff_eq_a1((t1[i]+0.5*a1_k2),t2[i],w1[i],w2[i])
a1_k4 = dt*diff_eq_a1((t1[i]+a1_k3),t2[i],w1[i],w2[i])
w1[i+1] = w1[i] + (a1_k1/6)+(a1_k2/3)+(a1_k3/3)+(a1_k4/6)
a2_k1 = dt*diff_eq_a1(t1[i],t2[i],w1[i],w2[i])
a2_k2 = dt*diff_eq_a1(t1[i],(t2[i]+0.5*a2_k1),w1[i],w2[i])
a2_k3 = dt*diff_eq_a1(t1[i],(t2[i]+0.5*a2_k2),w1[i],w2[i])
a2_k4 = dt*diff_eq_a1(t1[i],(t2[i]+a2_k3),w1[i],w2[i])
w2[i+1] = w2[i] + (a2_k1/6)+(a2_k2/3)+(a2_k3/3)+(a2_k4/6)
t1[i+1] = t1[i] + dt*w1[i]
t2[i+1] = t2[i] + dt*w2[i]
x1 = l1*np.sin(t1)
x2 = x1 + l2*np.sin(t2)
y1 = - l1*np.cos(t1)
y2 = y1 - l2*np.cos(t2)
I know its not very neat right now, but I really just want to get it working first

displaydata function in ex3 coursera machine learning

I am facing a issue, here is my script. some end or bracket issue but I have checked noting is missing.
function [h, display_array] = displayData(X, example_width)
%DISPLAYDATA Display 2D data in a nice grid
% [h, display_array] = DISPLAYDATA(X, example_width) displays 2D data
% stored in X in a nice grid. It returns the figure handle h and the
% displayed array if requested.
% Set example_width automatically if not passed in
if ~exist('example_width', 'var') || isempty(example_width)
example_width = round(sqrt(size(X, 2)));
end
% Gray Image
colormap(gray);
% Compute rows, cols
[m n] = size(X);
example_height = (n / example_width);
% Compute number of items to display
display_rows = floor(sqrt(m));
display_cols = ceil(m / display_rows);
% Between images padding
pad = 1;
% Setup blank display
display_array = - ones(pad + display_rows * (example_height + pad), ...
pad + display_cols * (example_width + pad));
% Copy each example into a patch on the display array
curr_ex = 1;
for j = 1:display_rows
for i = 1:display_cols
if curr_ex > m,
break;
end
% Copy the patch
% Get the max value of the patch
max_val = max(abs(X(curr_ex, :)));
display_array(pad + (j - 1) * (example_height + pad) +
(1:example_height), ...
pad + (i - 1) * (example_width + pad) +
(1:example_width)) = ...
reshape(X(curr_ex, :),
example_height, example_width) / max_val;
curr_ex = curr_ex + 1;
end
if curr_ex > m,
break;
end
end
% Display Image
h = imagesc(display_array, [-1 1]);
% Do not show axis
axis image off
drawnow;
end
ERROR:
displayData
parse error near line 86 of file C:\Users\ALI\displayData.m
syntax error
Pls guide which is the error in the script, this script is already written in
the coursera so its must be error free.
You seem to have modified the code, and moved the "ellipsis" operator (i.e. ...) or the line that is supposed to follow it, in several places compared to the original code in coursera.
Since the point of the ellipsis operator is to appear at the end of a line, denoting that the line that follows is meant to be a continuation of the line before, then moving either the ellipsis or the line below it will break the code.
E.g.
a = 1 + ... % correct use of ellipsis, code continues below
2 % treated as one line, i.e. a = 1 + 2
vs
a = 1 + % without ellipsis, the line is complete, and has an error
... 2 % bad use of ellipsis; also anything to the right of '...' is ignored
vs
a = 1 + ... % ellipsis used properly so far
% but the empty line here makes the whole 'line' `a = 1 +` which is wrong
2 % This is a new instruction

Octave index out of bounds

The program is an optimized gradient descent.
Here is the code :
clear all
close all
[x,y] = meshgrid(-2:0.1:2);
z = x.^2 + 100*y.^2;
n = 1;
k(n)=0.01;
arret = 0.0001;
mesh(x,y,z);
[x1(n),y1(n)] = ginput(1);
diff_x(n) = 2*x1(n);
diff_y(n) = 200*y1(n);
while sqrt(diff_x(n)^2 + diff_y(n)^2) > arret && n < 30
k(n) = sqrt(diff_x(n)^2 + diff_y(n)^2)/(8*x1(n).^2+2*10.^6*y1(n).^2);
x1(n+1) = x1(n) - k(n)*diff_x(n);
y1(n+1) = y1(n) - k(n)*diff_y(n);
n = n+1;
diff_x(n) = 2*x1(n);
diff_y(n) = 200*y1(n);
z1(n) = x1(n).^2 + 100*y1(n).^2;
plot3(x1(n),y1(n),z1(n));
end
x1(n)
y1(n)
n
So I got this and I don't understand why.
error: GradientPasOptFinal2: A(I): index out of bounds; value 2 out of bound 1
error: called from:
error: error: C:\Octave\octave-3.8.2\GradientPasOptFinal2.m at line 13, column 8
SOLVED : the k(n) between y1(n) and n was the reason, i don't know why, but now the program works, thank you !
You have only defined k(1). k(2) is undefined.
Move this line:
k(n) = sqrt(diff_x(n)^2 + diff_y(n)^2)/(8*x1(n).^2+2*10.^6*y1(n).^2);
Ahead of these lines:
x1(n+1) = x1(n) - k(n)*diff_x(n);
y1(n+1) = y1(n) - k(n)*diff_y(n);
And it should work.

int variable increment from function

I am trying to get the input from my user to assign a 'faction' and based on following inputs modify the values for curAdvRep\curCrmRep.
The input shows that indise the funtion i get the desired result, but I need to be able to permanently modify the rep for the faction.
From file 2 called in file 1:
curAdvRep = 0
curCrmRep = 0
Crimson = "Crimson Brootherhood reputation: {0}".format(curCrmRep)
Advent = "Advent of Chaos reputation: {0}".format(curAdvRep)
PathSelDict = {'Advent' : Advent, 'Crimson' : Crimson, 'n' : n, 'c' : cont, 'd' : d, 'p' : p, 'l' : l}
def Faction (rep):
global curAdvRep
global curCrmRep
global Advent
global Crimson
if rep in PathSelDict:
if rep == 'Advent':
curAdvRep += 50
curCrmRep -= 5
Advent = "Advent of Chaos reputation: {0}".format(curAdvRep)
print(Advent)
#print(Factions[JoinWorld])
elif rep == 'Crimson':
curAdvRep -= 5
curCrmRep += 50
print(PathSelDict[JoinWorld])
else:
print(Dismiss)
sys.exit(0)
From file 1:
rep = input("Which side are you on? Advent or Crimson? ").title()
questfunc.Faction(rep)
print(Advent)
print(curAdvRep)
print(curCrmRep)
Output:
Pick up the box or leave it alone? (p or l): p
Pick up the box
Reputation Gain
Advent of Chaos reputation: 5
Which side are you on? Advent or Crimson? advent
Advent of Chaos reputation: 55
Advent of Chaos reputation: 0
0
0
I am sorry if either my question or my code is offensive. I have researched an answer for my question, but due to either not finding a matching answer or my inability to translate an indirect answer to my specific question, I haven't found the solution.
So I found a work around. For those that have also asked this question and had no helpful response, please take a look.
Choice1 = input(" Pick up the box or leave it alone? (p or l): ").lower()
RepGain(Choice1)
print(Advent, curAdvRep)
# Function for gain
def RepGain (Choice):
global curAdvRep
global curCrmRep
global Advent
global Crimson
if Choice in PathSelDict:
if Choice == 'p':
print('Reputation Gain\nAdvent + 100')
curAdvRep += 100
return curAdvRep
elif Choice == 'l':
print('Crimson + 100')
curCrmRep += 100
return curCrmRep
else:
print(Dismiss)
sys.exit(0)
Output:
Reputation Gain
Advent + 100
Advent of Chaos reputation: 100