program in any language Numerical methods - numerical-methods

To estimate the absolute and relative error of the calculation of the F value under
condition of:
A) the given exact numbers of the entered values of the arguments are x1 , x2 , x3
B) the given values of arguments x1 , x2 , x3 with the error ϵ = N⋅10-3, where N is the
variant number
Variant 1
F = 2⋅x1 2 + 3⋅x2 2 + x3 2 + 4⋅x1⋅x2 – 3⋅x3 + cos(x2 - x1)
Variant 2
F = 5⋅x1 2 + 3⋅x2 2 + 2⋅x3 2 - 4⋅x2⋅x3 - 2⋅x1 – cos(x2⋅x3);

Related

How to solve functional equation?

I have functional equation
B(2z^4 + 4z^6 + 9z^8 + 20z^{10} + 44z^{12} + 96z^{14}) = (B(z))^4
I try to solve it using Maxima CAS :
(%i2) e: B(2*z^4 + 4*z^6 + 9*z^8 + 20*z^10 + 44*z^12 + 96*z^14) = (B(z))^4;
14 12 10 8 6 4 4
(%o2) B(96 z + 44 z + 20 z + 9 z + 4 z + 2 z ) = B (z)
(%i3) funcsolve (e,B(z));
expt: undefined: 0 to a negative exponent.
#0: rform(%r=[0,0])
#1: funcsol(%a=B(96*z^14+44*z^12+20*z^10+9*z^8+4*z^6+2*z^4) = B(z)^4,%f=B(z),l%=[])
#2: funcsolve(%a=B(96*z^14+44*z^12+20*z^10+9*z^8+4*z^6+2*z^4) = B(z)^4,%f=B(z))
#3: funcsolve(_l=[B(96*z^14+44*z^12+20*z^10+9*z^8+4*z^6+2*z^4) = B(z)^4,B(z)])
-- an error. To debug this try: debugmode(true);
Here simpler example :
define(f(z),z^2-1)
(%o3) f(z):=z^2-1
(%i4) f2:factor(f(f(z)))
(%o4) z^2*(z^2-2)
(%i5) e:B(f2) = B(z)^2
(%o5) B(z^2*(z^2-2)) = B(z)^2
(%i6) s:funcsolve(e,B(z))
expt: undefined: 0 to a negative exponent.
#0: rform(%r=[0,0])
#1: funcsol(%a=B(z^2*(z^2-2)) = B(z)^2,%f=B(z),l%=[])
#2: funcsolve(%a=B(z^2*(z^2-2)) = B(z)^2,%f=B(z))
#3: funcsolve(_l=[B(z^2*(z^2-2)) = B(z)^2,B(z)])
-- an error. To debug this try: debugmode(true);
How should I do it?
Is it another software / method for it ?

are there any pairs (x,y) such that x<<1 == y>>1?

Are there any pairs (x,y) such that z = x<<1 and z == y>>1 where x != y?
Suppose we have x=0010 and y=1000
now if we shift left x by 1 and shift right y by 1
i.e, x<<1=0100 and y>>1=0100 so we have same result for both cases that is 0100
So are there any other pairs (x,y) giving same result z ?
If yes then is there any equation to find out such pairs?
#doynax observed:
x << 1 = y >> 1 <=> x * 2 = floor(y / 2)
Using floor(y/2)==y/2 if y is even; floor(y/2)=(y-1)/2 if y is odd, we get two equations:
(even y): x * 2 = y / 2
(odd y): x * 2 = (y-1)/2
Simplifying:
(even y) x * 4 = y
(odd y) x * 4 = y - 1 ==> x * 4 + 1 = y
We remark that x * 2, and therefore x * 4, is an even number.
So any value of x has a corresponding y by the (even y) equation.
Likewise, x * 4 is even, and x * 4 + 1 is odd, so any
value of x has a corresponding y by the (odd y) equation.
So, these values are easy to generate.
Pick any X.
Both X*4 and X*4+1 are valid values for Y.
Z is X*2
This assumes infinite precision integers. If you are working on a real CPU with an N bit word size, values of X>=2^N/4 won't work as a typical shift will lose bits off the top.
Generally left shift of a number is (number^number_of_left_shifts) and right shift of a number is a (number/number_of_right_shifts)
you could try using the below equation,
x^1=y/2
y=2(x^1)
The above equation for only one left shift or right shift try substituting with the number_of_shifts you need
Substitute the a,b values such that it satisfies below equation
a^2=b/4
b=4(a^2)

Maxima - Substitutions in Equations - let() and letsimp() without effect

My own equation is a bit longer, but the following example shows perfectly where I struggle at the moment.
So far I have been using the let() and letsimp() function
to substitute longer terms in an equation,
but in this example they have no effect:
(%i1) eq: ((2*u+a^2+d) * y+x)/2*a = x;
2
a ((2 u + d + a ) y + x)
(%o1) ------------------------ = x
2
(%i2) let(2*u+a^2+d, %beta);
2
(%o2) 2 u + d + a --> %beta
(%i3) letsimp(eq);
2
a ((2 u + d + a ) y + x)
(%o3) ------------------------ = x
2
What is the preferred way to replace 2*u+a^2+d with %beta in this sample equation?
And why has letsimp() no effect?
Thank you very much!
letsimp applies only to "*" expressions. You could try subst.

f(n), understanding the equation

I've been tasked with writing MIPS instruction code for the following formula:
f(n) = 3 f(n-1) + 2 f(n-2)
f(0) = 1
f(1) = 1
I'm having issues understanding what the formula actually means.
From what I understand we are passing an int n to the doubly recursive program.
So for f(0) the for would the equation be:
f(n)=3*1(n-1) + 2*(n-2)
If n=10 the equation would be:
f(10)=3*1(10-1) + 2*(10-2)
I know I'm not getting this right at all because it wouldn't be recursive. Any light you could shed on what the equation actually means would be great. I should be able to write the MIPS code once I understand the equation.
I think it's a difference equation.
You're given two starting values:
f(0) = 1
f(1) = 1
f(n) = 3*f(n-1) + 2*f(n-2)
So now you can keep going like this:
f(2) = 3*f(1) + 2*f(0) = 3 + 2 = 5
f(3) = 3*f(2) + 2*f(1) = 15 + 2 = 17
So your recursive method would look like this (I'll write Java-like notation):
public int f(n) {
if (n == 0) {
return 1;
} else if (n == 1) {
return 1;
} else {
return 3*f(n-1) + 2*f(n-2); // see? the recursion happens here.
}
}
You have two base cases:
f(0) = 1
f(1) = 1
Anything else uses the recursive formula. For example, let's calculate f(4). It's not one of the base cases, so we must use the full equation. Plugging in n=4 we get:
f(4) = 3 f(4-1) + 2 f(4-2) = 3 f(3) + 2 f(2)
Hm, not done yet. To calculate f(4) we need to know what f(3) and f(2) are. Neither of those are base cases, so we've got to do some recursive calculations. All right...
f(3) = 3 f(3-1) + 2 f(3-2) = 3 f(2) + 2 f(1)
f(2) = 3 f(2-1) + 2 f(2-2) = 3 f(1) + 2 f(0)
There we go! We've reached bottom. f(2) is defined in terms of f(1) and f(0), and we know what those two values are. We were given those, so we don't need to do any more recursive calculations.
f(2) = 3 f(1) + 2 f(0) = 3×1 + 2×1 = 5
Now that we know what f(2) is, we can unwind our recursive chain and solve f(3).
f(3) = 3 f(2) + 2 f(1) = 3×5 + 2×1 = 17
And finally, we unwind one more time and solve f(4).
f(4) = 3 f(3) + 2 f(2) = 3×17 + 2×5 = 61
No, I think you're right and it is recursive. It seems to be a variation of the Fibonacci Sequence, a classic recursive problem
Remember, a recursive algorithm has 2 parts:
The base case
The recursive call
The base case specifies the point at which you cannot recurse anymore. For example, if you are sorting recursively, the base case is a list of length 1 (since a single item is trivially sorted).
So (assuming n is not negative), you have 2 base cases: n = 0 and n = 1. If your function receives an n value equal to 0 or 1, then it doesn't make sense to recurse anymore
With that in mind, your code should look something like this:
function f(int n):
#check for base case
#if not the base case, perform recursion
So let's use Fibonacci as an example.
In a Fibonacci sequence, each number is the sum of the 2 numbers before it. So, given the sequence 1, 2 the next number is obviously 1 + 2 = 3 and the number after that is 2 + 3 = 5, 3 + 5 = 8 and so on. Put generically, the nth Fibonacci number is the (n - 1)th Fibonacci Number plus the (n - 2)th Fibonacci Number, or f(n) = f(n - 1) + f(n - 2)
But where does the sequence start? This is were the base case comes in. Fibonacci defined his sequence as starting from 1, 1. This means that for our pruposes, f(0) = f(1) = 1. So...
function fibonacci(int n):
if n == 0 or n == 1:
#for any n less than 2
return 1
elif n >= 2:
#for any n 2 or greater
return fibonacci(n-1) + fibonacci(n-2)
else:
#this must n < 0
#throw some error
Note that one of the reasons Fibonacci is taught along with recursion is because it shows that sometimes recursion is a bad idea. I won't get into it here but for large n this recursive approach is very inefficient. The alternative is to have 2 global variables, n1 and n2 such that...
n1 = 1
n2 = 1
print n1
print n2
loop:
n = n1 + n2
n2 = n1
n1 = n
print n
will print the sequence.

Math - Get x & y coordinates at intervals along a line

I'm trying to get x and y coordinates for points along a line (segment) at even intervals. In my test case, it's every 16 pixels, but the idea is to do it programmatically in ActionScript-3.
I know how to get slope between two points, the y intercept of a line, and a2 + b2 = c2, I just can't recall / figure out how to use slope or angle to get a and b (x and y) given c.
Does anyone know a mathematical formula to figure out a and b given c, y-intercept and slope (or angle)? (AS3 is also fine.)
You have a triangle:
|\ a^2 + b^2 = c^2 = 16^2 = 256
| \
| \ c a = sqrt(256 - b^2)
a | \ b = sqrt(256 - a^2)
| \
|__________\
b
You also know (m is slope):
a/b = m
a = m*b
From your original triangle:
m*b = a = sqrt(256 - b^2)
m^2 * b^2 = 256 - b^2
Also, since m = c, you can say:
m^2 * b^2 = m^2 - b^2
(m^2 + 1) * b^2 = m^2
Therefore:
b = m / sqrt(m^2 + 1)
I'm lazy so you can find a yourself: a = sqrt(m^2 - b^2)
Let s be the slop.
we have: 1) s^2 = a^2/b^2 ==> a^2 = s^2 * b^2
and: 2) a^2 + b^2 = c^2 = 16*16
substitute a^2 in 2) with 1):
b = 16/sqrt(s^2+1)
and
a = sqrt((s^2 * 256)/(s^2 + 1)) = 16*abs(s)/sqrt(s^2+1)
In above, I assume you want to get the length of a and b. In reality, your s is a signed value, so a could be negative. Therefore, the incremental value of a will really be:
a = 16s/sqrt(s^2+1)
The Point class built in to Flash has a wonderful set of methods for doing exactly what you want. Define the line using two points and you can use the "interpolate" method to get points further down the line automatically, without any of the trigonometry.
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/geom/Point.html#interpolate()
The Slope is dy/dx. Or in your terms A/B.
Therefore you can step along the line by adding A to the Y coordinate, and B to the X coordinate. You can Scale A and B to make the steps bigger or smaller.
To Calculate the slope and get A and B.
Take two points on the line (X1,Y1) , (X2,Y2)
A= (Y2-Y1)
B= (X2-X1)
If you calculate this with the two points you want to iterate between simply divide A and B by the number of steps you want to take
STEPS=10
yStep= A/STEPS
xStep= B/STEPS
for (i=0;i<STEPS;i++)
{
xCur=x1+xStep*i;
yCur=y1+yStep*i;
}
Given the equation for a line as y=slope*x+intercept, you can simply plug in the x-values and read back the y's.
Your problem is computing the step-size along the x-axis (how big a change in x results from a 16-pixel move along the line, which is b in your included plot). Given that you know a^2 + b^2 = 16 (by definition) and slope = a/b, you can compute this:
slope = a/b => a = b * slope [multiply both sides by b]
a^2 + b^2 = 16 => (b * slope)^2 + b^2 = 16 [by substitution from the previous step]
I'll leave it to you to solve for b. After you have b you can compute (x,y) values by:
for x = 0; x += b
y = slope * x + intercept
echo (x,y)
loop