I wrote this function for the differential equation:
#odefun.m
function [dy] = odefun (t, y)
m = 10;
k = 100;
c = 20;
dy = zeros(2, 1);
dy(1) = y(2);
dy(2) = (-c*y(2) - k*y(1) + F(t))/m;
endfunction
function [Fvalue] = F(t);
F0 = 1;
omega = 2;
Fvalue = F0*cos(omega*t);
endfunction
And then in a separate file I try to plot:
tspan = 0:0.01:10;
y0 = [0 0];
[T Y] = ode45(#odefun, tspan, y0);
figure;
plot(T, Y);
legend('y..1', 'y..2');
xlabel(' time [s]');
When I run the program, I get an error:
error: 'y' undefined near line 8 column 9
error: called from
odefun at line 8 column 7
I took a sample code from the tutorial, everything works there, but but it doesn’t work for me. However, when I put everything into the same file, it does work. How can I make it work when using different files?
Related
I have a problem with my dynare/octave. When I run the code our professor uploaded (and which worked for him and my class mates, I get an error message. I found out that the error message comes from the function stoch_simul:
This is the error message.
DGGES : parameter number 19 is invalid
error: Fortran procedure terminated by call to XERBLA
Since the code should be fine, I thought it might be because of my dynare or octave. I reinstalled both but I still have the same result and tried it with Dynare 4.6.2 and 4.6.3. I have the Octave GNU version 5.2.0
I would appreciate any help!
This is my code:
var n y w i a nu r pi rast;
varexo eps_a eps_i;
parameters alpha, rho, theta, varphi, eta_a, phi_pi, phi_y, eta_i, epsilon, mu, piast, beta, kappa, psi;
alpha = 0.3; // production function
rho = 0.01; // time preference rate
varphi = 1; // inverse Frisch elasticity
theta = 1; // inverse elasticity of intertemporal substitution
eta_a = 0; // productivity shock persistence
phi_pi = 1.5; // interest rate rule parameter
phi_y = 0.125; // interest rate rule parameter
gamma = 0.667; // Calvo parameter
eta_i = 0.5; // monetary policy shock persistence
epsilon = 6; // elasticity of substitution
mu = epsilon/(epsilon-1); // mark-up
piast = 0.02; // inflation target
beta = 1/(1+rho+piast);
kappa = (1-gamma)*(1-gamma*beta)/gamma*(theta*(1-alpha)+varphi+alpha)/(1-alpha+epsilon);
psi = 1+(1-alpha)*(1-theta)/(theta*(1-alpha)+varphi+alpha);
model;
a = eta_a*a(-1) + eps_a;
nu = eta_i*nu(-1) + eps_i;
y = y(+1)-(1/theta)*(i-piast-pi(+1)-rast);
pi = beta*pi(+1) + kappa*y;
r = i - piast - pi(+1);
i = rho + piast + phi_pi*pi + phi_y*y + nu;
rast = rho + theta*psi*(a(+1)-a);
y = a + (1-alpha)*n;
w = theta*y + varphi*n;
end;
initval;
a = 0;
nu = 0;
pi = 0.02;
n = 0.3;
y = 0.6;
w = 1.5;
r = 0.05;
i = 0.03;
rast = 0.03;
end;
steady;
// check;
// Specify temporary shock
shocks;
var eps_a; stderr 0.0075;
var eps_i; stderr 0.003;
end;
stoch_simul(periods=200, drop=100, order=1, irf=12);
Say I have the following setup in my AS code:
var color:String = "#0000FF"; //blue
var alpha:Number = 42; //42% or 42/100
How would I combine those into a #RRGGBBAA hex color? I've been Googling around and trying to figure out hexadecimal conversion and notation without luck.
There are two ways you could do this.
One is a bit hacky using int's toString() method and passing 16 as the radix/base:
var rgb:int = (int)("#0000FF".replace("#","0x"));//convert the string to a int (note you can type hex ints starting with 0x (e.g. 0x0000FF)
var a:int = 42;
var rgba:int = int("0x"+rgb.toString(16) + a.toString(16));
or the less hacky and probably faster computationally method using bitwise operators:
var rgb:uint = (uint)("#0000FF".replace("#","0x"));
//extract components using bit shifting (>>) and masking (0xFF)
var r:uint = rgb >> 16 & 0xFF;
var g:uint = rgb >> 8 & 0xFF;
var b:uint = rgb >> 0 & 0xFF;//same as rgb >> 0xFF, just added >> 0 to make the shift obvious
var a:uint = 42;
var rgba:uint = r << 24 | g << 16 | b << 8 | a;
var argb:uint = a << 24 | r << 16 | g << 8 | b;
//test
trace(rgba.toString(16));
trace(argb.toString(16));
Note that using toString(16) in the trace above is to make it useful to us humans,
you'd use the actual uint value when working with hex color values.
Also note that you might want to use ARGB in as3 sometimes, for example when working with BitmapData:
addChild(new BitmapData(100,100,true,0x2a0000ff));//add a 42% transparent blue box (100x100 px)
UPDATE
The above bitshift code snippet actually explains rgb extraction in detail which helps understand things better, but you already have rgb, so it's a matter of adding the alpha component. Also you mentioned 42% which is not the same as on a 0 to 255 scale. Therefore bellow lies your answer:
var rgb:uint = (uint)("#0000FF".replace("#","0x"));
var a:uint = (uint)((42 * .01) * 255);//map 42 from 0<>100 to 0<>255 ( *.01 is the same as / 100 but faster
var rgba:uint = rgb << 8 | a;
var argb:uint = a << 24 | rgb;
Regarding speed, if I run the two different conversion methods a million times here are execution times:
using strings (var rgba:int = int("0x"+rgb.toString(16) + a.toString(16));) takes 851 ms
using bitwise ops (var rgba:uint = rgb << 8| a;) takes 3 ms
As you can the bitwise version is much faster and for your case even less verbose than the string version. Also, now that you understand bitwise operators probably it's easier to read/understand.
In conclusion:
var color:String = "#0000FF"; //blue
var alpha:Number = 42; //42% or 42/100
var rgb:uint = (uint)(color.replace("#","0x"));
var a:uint = (uint)((alpha * .01) * 255);
var rgba:uint = rgb << 8 | a;
trace("hex: #",rgba.toString(16),"test",0x0000ff6b.toString(16));
Also, it's funny you mentioned Google, because you can use the search to convert to hex.
Update:
There seems to be a bit of confusion so I've split the 3 steps into functions:
converting a hex string to an int
converting a alpha percentage (0-100) to a 0-255 int
concatenating the above
Which would be:
function getHex(hexStr:String):uint{
return (uint)(hexStr.replace("#","0x"));
}
function getHexAlpha(alpha:uint):uint{
return (uint)((alpha * .01) * 255);
}
function rgbaConcat(rgb:uint,a:uint):uint{
return rgb << 8 | a;
}
trace("test",rgbaConcat(getHex("#FF9900"),getHexAlpha(50)).toString(16));
or all in one go:
function rgbaConcat(hexStr:String,alpha:uint):uint{
var rgb:uint = (uint)(hexStr.replace("#","0x"));
var a:uint = (uint)((alpha * .01) * 255);
return (rgb << 8 | a);
}
trace("test",rgbaConcat("#123456",100).toString(16));
I'm not sure if sprintf or something similar is available in action script, but you would use something like:
var alpha_2:int = Math.round(255*alpha/100);
var rgba:String = sprintf("%s%2x", color, alpha_2);
By the way, be sure to check whether it is supposed to be #RRGGBBAA or #AARRGGBB
So apparently sprintf is not available, you can use some substitute as mentioned in Is there something like printf in Action Script 3?
If you do not like to use a printf like function you can use:
function hex_char(value:int) {
if (value < 0)
return "X";
if (value < 10)
return String.fromCharCode("0".charCodeAt(0)+value);
if (value < 16)
return String.fromCharCode("A".charCodeAt(0)+value-10);
return "X";
}
var alpha_2:int = Math.round(255*alpha/100);
var rgba:String = color + hex_char(alpha_2/16) + hex_char(alpha_2%16);
alternatively you coulde use the following definition for hex_char which (I assume) will give you an exception/error for any value under 0 or over 15 instead of "X"
function hex_char(value:int) {
return "0123456789ABCDEF".charAt(value);
}
I have a simple code that seems to be giving strange results.
var startPoint:Point = new Point(x, y); // a point
var r:Number = path[i].row + (-Math.floor((length * 2 + 2) / 2)); // just some math
trace(r); // the math checks out and gives a 3
var tey = startPoint.y + r; //this gives a really strange return....
trace(startPoint.y + r, tey); // this works, and then gives the strange return.
the return is
3
10 7function Function() {}
as
var tey:Number = startPoint.y + r;
trace(startPoint.y + r, tey);
the return is
10 NaN
IDE is FlashDevelop if you feel that information is important.
Additional Info/Testing:
trace(startPoint.y);
traces as
7
:Number
var tey:Number = startPoint.y + r;
trace(tey);
equals
NaN
Number()
var tey = Number(startPoint.y) + r;
trace(tey);
equals
7function Function() {}
(excuse my poor post layout, I'm still inexperienced with stackoverflow's editing system and am working on making this a little cleaner)
I can not reproduce your issue.
Your problem is with the X and Y values on this line, probably more so the Y value.
var startPoint:Point = new Point(x, y); // a point
Try hard coding the X and Y to 0
Here is some sample code I ran. This worked as expected, so I will assume its the vars X and Y or something related to the "i" when you access path[i].row
var startPoint:Point = new Point(0, 0); // a point
var r:Number = 3; // just some math
trace(r); // the math checks out and gives a 3
var tey = startPoint.y + r; //this gives a really strange return....
trace(startPoint.y + r, tey); // this works, and then gives the strange return.
I discovered the issue, it was with a part of the code that I appended out of my sample above.
var startPoint:Point = new Point(x, y); // a point
for ( /*for stuff*/ ){
var r:Number = path[i].row + (-Math.floor((length * 2 + 2) / 2)); // just some math
}
trace(r); // the math checks out and gives a 3
var tey = startPoint.y + r; //this gives a really strange return....
trace(startPoint.y + r, tey); // this works, and then gives the strange return.
being var'd inside of the for loop made it act oddly when used outside so I had to move the var assignment outside of the loop and use as normal. A strange occurrence, but I should have knew better.
I am a bit new to using blitting for graphics. But I have worked up a few demos myself, and I have been reading a lot of information on the methods used.
One common theme I have been seeing though is that all of them brute force rendering; drawing the farthest back object first and stepping through all other objects. even drawing objects that are going to be completely overlapped.
The reason all of them say this is that any kind of testing to see what should be drawn actually takes more time than just drawing everything with no checks.
Is there any kind of way to detect what should be drawn, that will run faster than just drawing everything?
It is hard to actually tell whether it is faster to check for what should be drawn or drawing stuff. You could maybe use both like If there is more than 5 images, use draw check, if not, draw them all. ...
So - the draw them all method is very obvious, and about the draw check it is like:
// drawCheck
var w:int = 300;
var h:int = 200;
var result:Bitmap = new Bitmap(w, h);
for (var x:int = 0; x < w; x++){
for (var y:int = 0; y < h; y++){
result.bitmapData.setPixel32(x, y, 0x00FFFFFF);
for (var iid:int = 0; iid < images.length; iid++){
var resC:uint = result.bitmapData.getPixel32(x, y);
var resA:uint = resC >>> 24;
var resR:uint = resC >>> 16 & 0xFF;
var resG:uint = resC >>> 8 & 0xFF;
var resB:uint = resC & 0xFF;
if (resA == 0xFF){
break;
}
var oriC:uint = images[iid].bitmapData.getPixel32(x, y);
var oriA:uint = oriC >>> 24 &;
var oriR:uint = oriC >>> 16 & 0xFF;
var oriG:uint = oriC >>> 8 & 0xFF;
var oriB:uint = oriC & 0xFF;
var newA:uint = resA + oriA;
var newR:uint = (256 / resA) * resR + (256 / oriA) * oriR;
var newG:uint = (256 / resA) * resR + (256 / oriA) * oriG;
var newB:uint = (256 / resA) * resR + (256 / oriA) * oriB;
var newC:uint = newA << 24 | newR << 16 | newG << 8 | newB;
result.bitmapData.setPixel32(x, y, newC);
}
}
}
Basically, MAYBE the drawing could be faster, but I am not sure - bitwise operations... Anyways - you should get the idea - this loops through X and Y coordinates and finally through the images.
Note: The images are stored like: 0 = front, images.length - 1 = back
It checks (HERE) if the resulting bitmap is already fully drawn by checking the alpha (if it equals 0xFF, there is no use of drawing), and if it is not, it merges the colours and adds the alpha.
You should do some performance tests so we know what is faster and when...
What is the best way to debug a CRASHING flash app ? (no exception, my application just crash)
I am actualy facing a big problem: my app (full-flash website) was working fine with the flashplayer 9 but crash with the flashplayer 10...
Here is the BAD method who crash my app with FP10.
After removing the call to this method everything was working properly with FP10.
public static function drawWedgeCrown(g : Graphics,a : Number,r : Number,r2 : Number, n : Number, c : Number, t : Number) : void {
var x : Number ;
var y : Number;
g.beginFill(c, t);
g.moveTo(r, 0);
g.lineTo(r, 0);
var teta : Number = 0;
var dteta : Number = 2 * Math.PI / n;
while(teta < a) {
x = r * Math.cos(teta);
y = -r * Math.sin(teta);
g.lineTo(x, y);
teta += dteta;
}
x = r * Math.cos(a);
y = -r * Math.sin(a);
g.lineTo(x, y);
x = r2 * Math.cos(a);
y = -r2 * Math.sin(a);
g.lineTo(x, y);
teta = a;
dteta = 2 * Math.PI / n;
var cpt : int = 0;
while(teta > 0) {
cpt++;
x = r2 * Math.cos(teta);
y = -r2 * Math.sin(teta);
g.lineTo(x, y);
teta -= dteta;
}
x = r2 * Math.cos(0);
y = -r2 * Math.sin(0);
g.lineTo(x, y);
g.lineTo(r, 0);
g.endFill();
}
OK, i finaly found the real PROBLEM... it was not the method in it self.
I was passing NaN for the "A" argument causing an infinite loop...
Have you tried running it with the debugger? Set a breakpoint at the entry of your app and then step through it until it crashes. This way you can see which line of code is responsible and the state of the variables. Of course the actual problem might be something that happens prior but at least you have narrowed down your search and can trace backwards.
Also another way is to put some trace() statements in your code and see if the section ever gets hit. Then you can tell if its happening before or after and repeat until you find the problem area.