Cost function is modified to have generic dimension for output vector y instead of having hardcoded one