I suck at programming.
I'm not a good student.

From here on I'll be posting about software security using the hashtag

Newton-Raphson

13 min read

Método de Newton-Raphson???? Gilberto Rogel García ?? A01630171

--Introducción y antecedentes (para qué sirve y qué resuelve) del método

De las fórmulas para localizar raíces, la fórmula de Newton-Raphson es de las más ampliamente utilizadas. Si el valor inicial para la raíz es xi, entonces se puede trazar una tangente desde el punto [xi, f(xi)] de la curva. Por lo común, el punto donde esta tangente cruza al eje x representa una aproximación mejorada de la raíz.

--En qué consiste el método

El método de Newton-Raphson se deduce a partir de esta interpretación geométrica. Se tiene que la primera derivada en x es equivalente a la pendiente:

??????????? f’(xi)= ( f(xi) -0 ) / ( xi –(xi+1) )

que se arregla para obtener

??????????? xi+1= xi – ( f(xi) / f’(xi) )

--Requisitos previos del método

Una función y su derivada.

--Diagrama de flujo

?dy888神马午夜

--Criterio de detención del método (error)

El error debe ser proporcional al cuadrado del error anterior. En otras palabras, el número de cifras significativas de precisión aproximadamente se duplica en cada iteración.

--Código fuente

"stdafx.h"

<iostream>

<cmath>

<iomanip>

<Windows.h>

?

?

PRECISION 4

MAX_ITERACIONES 100

INTERVALOS 6

?

using namespace std;

?

void tabula(double a, double b, int intervalos);???

double f(double x);

double f_derivada(double x);

void newton_raphson(double x0, double tolerancia, int max_interaciones);???

?

?

int main()

{

??????????? double a;

??????????? double b;

??????????? double tolerancia;??

??????????? double x0;

?

??????????? cout << setprecision(PRECISION);???

??????????? cout << "\nEscoja el punto inicial adecuado:?? x0 = ";

??????????? cin >> x0;

?

???????????

??????????? cout << "Tolerancia = ";

??????????? cin >> tolerancia;

?

?

??????????? newton_raphson(x0, tolerancia, MAX_ITERACIONES);

?

??????????? cin.get();

??????????? cin.get();

??????????? return 0;

}

?

?

void tabula(double a, double b, int intervalos)

{

??????????? int puntos = intervalos + 1;

?

??????????? double ancho = (b - a) / intervalos;

?

??????????? cout << "\n\tx\t\tf(x) " << endl;

??????????? for (int i = 0; i < puntos; i++) {

??????????????????????? cout << "\t" << a << "\t\t" << f(a) << endl;

??????????????????????? a = a + ancho;

??????????? }

}

?

?

double f(double x)

{

???????????

??????????? return exp(x) - pow(x, 2) + (3 * x) - 2;

}

?

?

double f_derivada(double x)

{

???????????

??????????? return exp(x) - (2*x) + 3;

}

?

?

void newton_raphson(double x0, double tolerancia, int max_iteraciones)

{

??????????? double x1;

??????????? double error;???

??????????? int iteracion;

??????????? bool converge = true;

?

???????????

??????????? cout << "\nAproximacion inicial:\n";

??????????? cout << "x0 = " << x0 << "\n"

??????????????????????? << "f(x0) = " << f(x0) << "\n"

??????????????????????? << "f'(x0) = " << f_derivada(x0) << endl;

?

??????????? iteracion = 1;

??????????? do {

?

??????????????????????? if (iteracion > max_iteraciones) {

??????????????????????????????????? converge = false;??

??????????????????????????????????? break;

?

??????????????????????? }

??????????????????????? else {

??????????????????????????????????? x1 = x0 - f(x0) / f_derivada(x0);

??????????????????????????????????? error = fabs(x1 - x0);?

?

???????????????????????????????????????????????????????????????????????????????????????????????????????????

??????????????????????????????????? cout << "\a";

???????????????????????????????????

??????????????????????????????????? cout << "\n\nIteracion #" << iteracion << endl;

??????????????????????????????????? cout << "\nx" << iteracion << "???? = " << x1 << "\n"

??????????????????????????????????????????????? << "f(x" << iteracion << ")? = " << f(x1) << "\n"

??????????? ??????????????????????????????????? << "f'(x" << iteracion << ") = " << f_derivada(x1) << "\n"

??????????????????????????????????????????????? << "error? = " << error << endl;

?

???????????????????????????????????

??????????????????????????????????? if (error <= tolerancia) {

??????????????????????????????????????????????? converge = true;

??????????????????????????????????????????????? break;

?

???????????????????????????????????????????????

??????????????????????????????????? }

??????????????????????????????????? else {

??????????????????????????????????????????????? x0 = x1;

??????????????????????????????????????????????? iteracion++;

??????????????????????????????????? }

??????????????????????? }

?

??????????? } while (1);

?

?

??????????? cout << "\a";

??????????? if (converge) {

??????????????????????? cout << "\n\nPara una tolerancia de " << tolerancia << " la RAIZ APROXIMADA de f es = " << x1 << endl;

?

??????????? }

??????????? else {

??????????????????????? cout << "\n\nSe sobrepasó la máxima cantidad de iteraciones permitidas" << endl;

??????????? }

}

--Pruebas y resultados con casos de éxito, casos de falla y casos frontera

Cuando probamos el método de Newton-Raphson con la función f(x) = tan-1(x) - .3, el método diverge si no tomas un valor inicial cercano a la raíz, en la imagen por ejemplo tome el valor x = 3

?

?

Cuando tomas un valor cercano a la raíz, en este caso x = 1 , el método encuentra la raíz:

?

--Conclusiones

El método de Newton-Raphson algunas veces no converge, sino que oscila. Esto ocurre si no hay raíz real, si la raíz es un punto de inflexión o si el valor inicial esta muy alejado de la raíz buscada? y alguna otra parte de la función “atrapa” la iteración.

Cuando el método de Newton-Raphson converge, se obtienen resultados en relativamente pocas interacciones, ya que para raíces no repetidas este método converge con orden 2 y el error Ei+1?es proporcional al cuadrado del resultado anterior Ei.

?

Referencias:

http://www.bragitoff.com/2015/10/newton-raphson-lab-write-up-with-algorithm-and-flow-chart/

?

http://test.cua.uam.mx/MN/Methods/Raices/NewtonRaphson/NewtonRaphson.php

Minigames Project

2 min read

For our project we decided to make 2 games, one that will run in the Eclipse console and another one with a graphic user interface. First we decided the two games that we would do, Hangman and Tic Tac Toe. We decided to make Hangman run in the console since Tic Tac Toe would look kind of weird. The Hangman we did can be played by 2 players, in which one chooses the word the other player will try to guess in 6 attempts. The Hangman was pretty easy and we ended up finishing it in 2 days. Then we started doing the Tic Tac Toe, it was overall a kind of extensive process. At first we planned the logic of the game, how to win, etc. We decided that the best option would be to make vectors on the positions that you can put 'X' on 'O' in order to win. Then we thought that it would be better to play vs the computer, since it's really easy to counter the player moves on the game. So basically we did a multiplayer game and a one player one.?

We first tried to make a menu but we didnt know how to link to the minigames so we removed it. This is what it looked like:?

?

?

?

?

?

dy888神马午夜

Tic-Tac-Toe

In the process of doing our project we learned GUI(Graphical User Interface) implementations, we learned to use arrays, how a GUI works and apply them in the project, also we learned how to divide the work and connect it in Github.

"Making this project was really interesting, because it was one of the first times that I could translate cool ideas (like a videogame) into coding, you really start to notice that programming is like a second logic, another way of thinking. The Hangman was actually easier than we originally thought, but the Tic Tac Toe was a bit harder to implement in a GUI factor. We had to research quite a few things in order to fully be capable to finish the game but we managed to do it. It was really awesome and I wish I had more projects like this throughout my degree."

-Oscar López, 2016.

Luis Eduardo Vargas Victoria A01630086

Oscar Ricardo López López A01229116

Itzel cordero Sánchez A01113123

Gilberto Rogel García A01630171

Metaobject Protocol

1 min read

In?computer science, a?metaobject?is an?object?that manipulates, creates, describes, or implements other objects (including itself). The object that the metaobject is about is called the base object.

A?metaobject protocol?(MOP) provides the vocabulary to access and manipulate the structure and behavior of objects. Typical functions of a metaobject protocol include:

  • Creating and deleting new classes
  • Creating new methods and properties
  • Changing the class structure so that classes inherit from different classes
  • Generating or modifying the code that defines the methods for the class

Source: Wikipedia.

Course Review

2 min read

This course is not for the weak nor the procrastinators.

I think flipped learning and is good for students who actually want to learn because it gives you the choice to either procrastinate and waste time or organize yourself and your time to do the activities, homework, learn OOP, meet new people who can help you with OOP which is mostly what i did during this course (of course I did procrastinate a little, and by a little i mean ALOT, but I did everything, or at least most of what I was asked to do and some extra stuff which helped me with other courses).

During this course i learned Object Oriented Programming in the almighty JAVA programming language, and how did i did this? Well, first of all, without the help of my classroom partners or Lynda videos/StackOverflow/random websites i wouldn't be able learn the basic concepts of OOP such as what is an object, method, inheritance, polymorphism, delegation, use cases, a little of graphical user interface, etc.

I liked this way of learning mostly because it encouraged me to help myself and not procrastinate, i also liked it because i could ask anyone in the class about any problem i had and i would get help and answers real quick which helped me get my assignments done. I also had a lot of fun reading my classmates blog posts, some of them where funny, sarcastic and they really helped me with my WSQs and learning.

?

Gilberto Rogel García A01630171

sorryforthebadenglish:)

WSQ13 – James Gosling

1 min read

Sir?James Gosling or best known as the father of the Java programming language.

So this guy started programming at age 14 because he broke into a --insert university's name here because i don't remember it-- data center and he kept going to that university to read and learn about the machines in that room and noone ever noticed because he figured out that if you pretend you're supposed to be there everyone else will think that indeed you're supposed to be there. When i was age 14 i didn't know what was i doing with my life but i never did something as clever as breaking into somewhere to learn stuff... this guy's crazy smart! But that's not the point here, the point is that he invented the almighty JAVA PROGRAMMING LANGUAGE.?

End of thinking capacity...

Gilberto Rogel García A01630171

?

?

WSQ12 Ward Cunningham

1 min read

"It's great to be here" You can see his face full of emotion.

The great Ward Cunningham, inventor of the Wiki, started with a little database made of cards using Hyper Card(It combines databse?capabilities with a graphical, flexible, user-modifiable interface.) because his goal was the absortion of technology amongts technologist, and he made a database out of cards that tracked how ideas moved throught his company.

I find it really impressive?how can just one person think of an idea to help himself and his company and it ends up being one of the most used tools amongts students and researchers or whoever uses it to find any kind of information, like, almost all kinds of concepts and information can be found there.

At some point of the interview the guy asked what should students learn or something like that and Ward replied that we should learn what a boolean variable is (-.-), jquery and javascript. Now i got more things to do during the summer.

Gilberto Rogel García ? A01630171

WSQ11 Library Assignment

2 min read

Of course I'm stupid enough to not to read instructions so i went ahead and made the GravityCalculator Assignment...?Code in github

after doing this i went on and read the instructions "again" and noticed i only had to do the Library Assignment,?which asked to make two classes: Book & Library. Both classes had a bunch of methods to complete their task, but they were empty and what this assignment asked for was basically to fill in the blanks of every method to fullfil their function.

In the Book class for example it had:

public class Book {String title;
boolean borrowed; 
// Marks the book as not rentedpublic void returned() {// Implement this method?

}

}

And you just had to write the implementation inside the method. In this case it's easy because it only asks you to mark the book as not rented so the result should look like this:

// Marks the book as not rentedpublic void returned() {this.borrowed=false;

}?

Final result of the class Book:?Github link

The class Library was a bit more difficult, at first i didnt understand how should i implement them methods but with a lil help from Jorge Padilla's Blog and Luis Vargas's Blog i noticed that a book array?made this assignment (and your life) less hard.

?

Result (NOT FINAL) of the class Library: ?Github Link

HOW THE OUTPUT SHOULD BE:

HOW MY OUTPUT IS:?

As you can see i have 2 mistakes:?

  1. "Sorry, this book is not in our catalog" missing.
  2. The Lord of the Rings book shouldnt be available in the first library.

I couldnt figure out how to solve these mistakes... any help is appreciated!!.?

Gilberto Rogel García A01630171

Encuesta de Opinión de Alumnos (ECOA)

1 min read

I destroyed some?of my teachers. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?JK.

Gilberto Rogel García - a01630171

Can't stop procrastinating. (Second Partial Questions Review)

4 min read

After about 13 hours of trying to start studying i finally managed to do so. Here's a little study guide i made from the help of the Internet and other people's blogs: (INHERITANCE AND POLYMORPHISM ARE ON MY 1ST PARTIAL STUDY GUIDE POST)

Delegation: When an object receives a request, the object can either handle the request itself or pass the request on to a second object to do the work. If the object decides to pass the quest on, you say that the object has forwarded responsibility for handling the request to the second object.

CRC Cards: Class, Responsibilities, collaborators. Although CRC cards were originally introduced as a technique for teaching object-oriented concepts, they have also been successfully used as a full-fledged modeling technique.

Class: represents a collection of similar objects.

Responsibilities: something that a class knows or does.

Collaborators: another class that a class interacts with to fulfill its responsibilities

Overloading (compile time) and overriding (runtime): The most basic difference is that overloading is being done in the same class while for overriding base and child classes are required.

OVERLOADING: Method overloading in Java occurs when two or more methods in the same class have the exact same name but different parameters.

Also a valid case of overloading, since the?? //parameters are of different types:???

int changeDate(float Year) ;?

int changeDate(int Year);?

OVERLOADING EXAMPLE

//A class for adding upto 5 numbers

class Sum {

??? int add(int n1, int n2) {

??????? return n1+n2;

??? }

??? int add(int n1, int n2, int n3) {

??????? return n1+n2+n3;

??? }

??? int add(int n1, int n2, int n3, int n4) {

??????? return n1+n2+n3+n4;

??? }

??? int add(int n1, int n2, int n3, int n4, int n5){

??????? return n1+n2+n3+n4+n5;

??? }

??? public static void main(String args[]){

??? ???? Sum obj = new Sum();

??? ???? System.out.println("Sum of two numbers: "+obj.add(1, 2)); //3

??? ???? System.out.println("Sum of three numbers: "+obj.add(1, 2, 3)); //6

??? ???? System.out.println("Sum of four numbers: "+obj.add(1, 2, 3, 4)); //10

??? ???? System.out.println("Sum of five numbers: "+obj.add(1, 2, 3, 4, 5)); //15

??? }

}

Here we have 4 versions of same method “add” which means you’re overloading the method add().

OVERRIDING: An overridden method would have the exact same method name, return type, number of parameters, and types of parameters as the method in the parent class, and the only difference would be the definition of the method.

OVERRIDING EXAMPLE

class Car{
?? ?public int speedLimit(){
?????? ?return 100;
?? ?}
}
class lilCar extends Car{
?? ?public int speedLimit(){
?????? ?return 150;
?? ?}
?? ?public static void main(String args[]){
??? ???? Car obj = new lilCar();
??? ???? int num= obj.speedLimit();
??? ???? System.out.println("Speed Limit is: "+num); //150
?? ?}
}

Here speedLimit() method of class lilcar is overriding the speedLimit() method of class CarClass.

  • What do you feel are the benefits to object-oriented programming?
  • Why do you think your change to object-oriented programming is difficult or what makes it difficult for the standard student? ?

These two questions are more subjective so i'll let them be.?

Usefull Links:?Delegation? ??CRC Cards? ??Overloading and overriding? ? ??More overloading and overriding

?

Gilberto Rogel García a01630171