I have a function to evaluate and I want to graph it point by point. But the function only gives me a value and not the set of evaluated values.
rho = 0.999;
n = -10:1:10;
x = (rho.^(n))*(1 - rho)
y = 1 - (rho.^(n + 1))
Pb = x/y
plot(n, Pb)
x is a 1x21 double array just like y but my evaluated function Pb is a single value, not a set of evaluated values. Therefore I do not get my graph
What is the problem with the code?
I'm assuming you want to divide each element of x by its corresponding y? Fix the line
Pb = x./ y;
Related
I have to analyze what some code in C does, and I have a doubt about what happens in a certain line. The code is
#define PRINTX printf("%d\n", x)
void problem() {
int x = 2, y, z;
x *= 3 + 2; PRINTX;
x *= y = z = 4; PRINTX;
x = y == z; PRINTX;
x == (y = z); PRINTX; // This line is the problem
}
This code snippet prints the resulting numbers:
10
40
1
1 // This result **
the problem is that I'm still trying to figure out why does the last line prints out x = 1, when the operation is x == (y = z). I'm having trouble finding out what that 1 means and the precedence of the operations. Hope someone can help me! :)
Nothing in the last statement changes the value of x, so its value remains unchanged.
Parens were used to override precedence, forcing the = to be the operand of the ==.
An operator's operands must necessarily be evaluated before the operator itself, so we know the following:
y is evaluated at some point before the =.
z is evaluated at some point before the =.
x is evaluated at some point before the ==.
= is evaluated at some point before ==.
That's it. All of these are valid orders:
z y = x ==
y z = x ==
x y z = ==
etc.
But whenever x, y and z are evaluated, we can count on the following happening:
= assigns the value of z (currently 4) to y and returns it.
== compares the value of x (currently 1) with the value returned by = (4). Since they're different, == returns 0 (which isn't used by anything).
As you see, nothing changed x, so it still has the value it previously had (1).
In the last statement, nothing is changing the value of x. We are testing if x equals something, but we aren't changing it's value.
So it continues having the same value as it had in the previous statement, in particular, a value of 1.
the reason is because the == operator checks if the 2 numbers are equal, and returns 1 if equal and 0 if not equal that is why it returns one you can check by making x= 1 and y=2 and using the == operator between them
The comparison result of x and assignment of y with (y = z) is discarded. Last line could have dropped the compare: y = z; PRINTX;.
The assignment is not subsequently used either, so the line could have been PRINTX;.
I'm first year student in Software Engineering and in my last task I used (chain assignment) multiple assignment of values.
The lecturer claims that it is illegal to assign values in this way.
X = Y = Z;
(The three variables are declared in the beginning of the function).
I would be happy if you can explain to me if the assign is right action and if what the teacher claims is right.
It depends on whether all of the objects were correctly declared and Z was initialized or assigned with an appropriate value before this statement or not.
You can even assign values of objects of different types due to implicit casting, but the object which is getting assigned needs to be capable of holding the value of the object which is assigned to this one.
If Z was initialized or assigned before and all objects were correct declared, then:
X = Y = Z;
is completely correct and legal as it assigns the value held in Z to Y and X - the assignment takes place from right to left.
For example:
int X,Y,Z; // All objects are declared.
Z = 24; // `Z` is assigned with the integer value 24.
X = Y = Z; // `X` and `Y` get assigned by 24 which is the value in `Z`.
Else if Z has an indeterminate value (f.e. if it was not declared with the extern or static keyword or declared at global scope) and you would assign this value to Y and X with that statement.
Although the assignment itself would be even then not illegal, if you would use one of the assigned objects the program would give undefined results/output.
Speaking for the multiple assignments only, there is nothing illegal about it, you could even use:
O = P = Q = R = S = T = U = V = W = X = Y = Z;
if all objects used were correctly declared before and Z has an determined value.
The lecturer claims that it is illegal to assign values in this way.
If it is possible, I would ask her/him or your teacher, what s/he meant with that. Maybe there is something specific to your course you should care about.
Almost every expression has a value.
An assignment is an expression with value.
The value of the expression i = 42 is 42.
Assignment associates right to left, so the expression a = b = c is the same as a = (b = c).
So
int i;
double d;
char c;
c = i = d = 42;
Converts 42 to double and assigns to d. The value of this assignment is 42.0.
Then this value is converted to int and assigned to i. Value of this assignment is 42.
Lastly the int value is converted to char and assigned to c.
c = (i = (d = 42));
It's legal, for example:
float x;
int y;
float z = 3.5;
x = y = z;
it's similar to y = z; x = y;. So in this case, z = 3.5, y = 3 and x = 3;
But it will give the warning if you did not initialize the value of z.
int x,y, z;
x = 0;
y = 1;
//z = 2;
x = y = z;
Together with right-associativity of evaluation, and assuming non-volatile a, b, and c, it means that a = b = c is equivalent to a = (b = c), and again equivalent to b = c; a = b
Assigning values to multiple variables in a single line is perfectly alright and is allowed by C programming language.
However, the use of this style is usually discouraged because it may cause undesirable side-effects in some cases.
The expectation of Mathematical equation X = Y = Z may be:
Y = Z and X = Z (I.E., Assign the value of Z to both X and Y)
But C language treats the multiple assignments like a chain, like this:
In C, "X = Y = Z" means that the value of Z must be first assigned to Y, and then the value of Y must be assigned to X.
Here is a sample program to see how the multiple assignments in single line work:
#include <stdio.h>
int main() {
int n;
int X, Y, Z;
printf("Enter an integer: ");
scanf("%d", &n);
printf("You entered: %d\n", n);
printf("Performing multiple assignments:\n X = Y = Z = n++ \n");
X = Y = Z = n++;
printf("n = %d\n", n);
printf("X = %d \n", X);
printf("Y = %d \n", Y);
printf("Z = %d \n", Z);
return 0;
}
Output:
Enter an integer: 100
You entered: 100
Performing multiple assignments:
X = Y = Z = n++
n = 101
X = 100
Y = 100
Z = 100
Points to note in the above example:
The line X = Y = Z = n++ got processed like this:
Step 1: Assign the value of n (100) to Z
Step 2: Increment the value of n (n becomes 101)
Step 3: Assign the value of Z to Y (Y becomes 100)
Step 4: Assign the value of Y to X (X becomes 100)
Conclusion:
'Multiple assignments in single line' is a supported style. It has its benefits.
However, if the programmer is not aware of the sequence of operations involved in the multiple assignments statement, then it can lead to inconsistency between the expectation of the person who reads the program and the actual result of the execution.
To avoid this scenario, the multiple assignments are discouraged.
There's nothing illegal about it, provided each variable is only modified once. For example, the following all have well-defined behavior:
x = y = z;
x = y = z = x + 1;
x = y++ + --z;
x += (y = (z += 1)) - 6;
But the following have undefined behavior, and should be avoided:
x = y = x = z;
x = x++;
x = y++ + y++
x = func(y++, y++);
fsolve solves a problem specified by
F(x)=0
for x. In my case, x is a 2-dimensional array of size m x n. Now suppose
that in addition to x, there is a vector y that needs to be solved for
as well. What is the best way to expand the number of variables fsolve
solves for, given that fsolve only allows for a single argument?
One solution, one that I would like to avoid, is to rewrite the problem in
terms of a single vector with entries of x and y stacked as in
x(1) ... x(m x n), y(1) ... y(m-1)
I would like to avoid this solution because the equations for the first set
of variables can be nicely defined in 2 dimensions.
Would it be possible to create a 3-dimensional array of size m x n x 2 where the first page (the 3rd dimension in Matlab terminology) is the original x-matrix and the entries of y appear on the second page? But then, what is if y is only of size m-1 so that the second page is largely empty? Would this be possible somehow?
EDIT
Here is a simplied version of the model with m=n=2.
The equations in the first set are
x(1,1)*dot(k1,y)-y(1) = 0;
x(1,2)*dot(k2,y)-y(1) = 0;
x(2,1)*dot(k1,y)-y(2) = 0;
x(2,2)*dot(k2,y)-y(2) = 0;
where k1 and k2 are two vectors of parameters.
When m=n=2, the second set of equations consists of a single equation of the form
y(1)*dot(c1,x(1,:)) + y(2)*dot(c2,x(2,:)) = 0
Where c1 and c2 are two vectors of parameters. The last entry of y, here y(2), is always a parameter.
You can combine them into a single vector but you don't need to modify F. For example suppose you have a function F(x, y) where x and y are arbitrarily sized then something like the following should work
function [x, y] = fsolve2(F, x0, y0)
% flatten x0 and y0 into a single column vector
xy0 = [x0(:); y0(:)];
% utility functions for recovering x and y from combined vector
xstart = 1;
xend = xstart + numel(x0) - 1;
xsize = size(x0);
getx = #(xy) reshape(xy(xstart:xend), xsize);
ystart = xend + 1;
yend = ystart + numel(y0) - 1;
ysize = size(y0);
gety = #(xy) reshape(xy(ystart:yend), ysize);
% define G which takes xy and calls F
G = #(xy) F(getx(xy), gety(xy));
xy = fsolve(G, xy0);
% unwrap xy into x and y
x = getx(xy);
y = gety(xy);
end
which could be called with
x, y = fsolve2(F, x0, y0);
// Assuming these definitions
int x;
float y;
What is the difference between this:
x = y = 7.5;
and this:
y = x = 7.5;
How come the first one prints y value as 7.5,
and second one prints y as 7.00?
The explanation is very simple: = is right to left associative, which means x = y = 7.5; is evaluated as x = (y = 7.5); hence essentially the same as:
y = 7.5; // value is converted from double to float, y receives 7.5F
x = y; // value of y is converted from float to int, x receives 7 (truncated toward 0)
Whereas y = x = 7.5; is evaluated as y = (x = 7.5);:
x = 7.5; // 7.5 is converted to int, x receives value 7 (truncated toward 0)
y = x; // value of x is converted to float, y receives 7.0F
These implicit conversions can be counter intuitive. You might want to increase the warning level to let the compiler warn you about potential mistakes and unwanted side effects.
An assignment chain like you show, is evaluated from right to left.
x = y = 7.5;
is the same as
x = (y = 7.5);
Furthermore the result of an assignment is the assigned value.
This means that 7.5 (type double) is implicitely cast to float and then assigned to y. The result (7.5f) is then assigned to x. During this assignment the value is cast to int and the result is 7 which is stored in x.
If you switch the order you get different types:
y = x = 7.5;
Now 7.5 (type double) is implicitely cast to int and then assigned to x. The result is 7 which is assigned to y. Now that value is cast to double but the fraction is already lost and you will get 7.0f being stored in y.
In second expression
y = x = 7.5; /* multiple assignment operator. R->L associativity */
x = 7.5 evaluates first and you are assigning a real floating value 7.5 to an integer x which leads to truncation of fractional part, hence x gets assigned with 7 instead of 7.5 and later on y gets assigned with 7.00000
From the C99 Standard section 6.3.1.4 Real floating and integer
When a finite value of real floating type is converted to an
integer type other than _Bool, the fractional part is discarded (i.e., the value is truncated toward zero). If the value
of the integral part cannot be represented by the integer type,
the behavior is undefined.
#include<stdio.h>
main()
{
int x = 5, y = 10, z = 10;
x = y == z; // This computational expression causes the value of x to be 1. I fail to understand why
printf("%d\n", x); //Why is the value of x 1 here.
}
I fail to understand the statement x = y ==z;
According to me - x = 10 since y == z. z=10 and is stated to be equivalent to y. The value of y is then assigned to x - x = y
You assign the result of the comparison »Is y equal to z« to x, which is 1, i.e. true.
Note the different operators:
x = ... // assignment
y == z // comparison with either 0 (false) or 1 (true) result
Let's break the program down a little bit further:
You initialize x to 5 and y and z to 10.
You perform a comparison (see above) of y and z without caring for the result. So that's a line that can safely be ignored. But it results in 1 since y is equal to z.
You print the current values of all three variables.
You perform the same comparison, this time assigning the result to x. x now has the value »Is y equal to z«, which is 1 in this case.
Because of operators precedence
x = y == z;
is the same as
x = (y == z);
Now as y == z evaluates to 1, so x value is 1 after the statement.
y == z returns 1 if they are the same and 0 if they are not and the result of this is set to x
== is a comparison operator, so will return 1 (true) if both of the operands are equal and 0 (false) if they are not.
The statement x = y == z is equivalent to x = (y == z), because == has a higher precedence than =. Because y is equal to z, this will assign 1 to x.
Please refer to Operator Precedence Table. == (Comparison Operator) has a higher precedence over = (Assignment Operator). So, the y == z gets executed first and then yields result of 1 as y and z are having the same values which results in x being assigned a value of 1.
= is the assignment operator and == is the comparison operator. you compare y and z, they are equal, so the comparison returns true which is 1. this value is assigned to x.
the == operator acts first.
Hence it becomes x= (value of y==z);
now since y and z are the same, the value of y==z is 1 that gets assigned to x.
== has a greater precedence than =.
The expression y == z is evaluated to 1.
The instruction x = y == z puts 1 in x.
The instruction printf("%d\n", x); prints the value of x (1).
The precedence of == operator is higher than = operator.
y==z evaluates to 1; since both are equal.
Thisn value gets assigned to x.