Click here to Skip to main content
15,851,711 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
here is the coding i need seems like got some error and someone can fix it ?

i couldn't get value x and cant plot the graph

x is coefficient

Python
import sys
import matplotlib.pyplot as plt
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from PyQt5.QtWidgets import QHBoxLayout
from PyQt5 import QtCore, QtGui, QtWidgets

class Ui_MainWindow(object):
   def setupUi(self, MainWindow):
       MainWindow.setObjectName("MainWindow")
       MainWindow.resize(800, 600)
       self.centralwidget = QtWidgets.QWidget(MainWindow)
       self.centralwidget.setObjectName("centralwidget")
       self.label = QtWidgets.QLabel(self.centralwidget)
       self.label.setGeometry(QtCore.QRect(230, 0, 331, 41))
       font = QtGui.QFont()
       font.setPointSize(12)
       font.setBold(True)
       font.setWeight(75)
       self.label.setFont(font)
       self.label.setObjectName("label")
       self.pushButton = QtWidgets.QPushButton(self.centralwidget)
       self.pushButton.setGeometry(QtCore.QRect(690, 440, 91, 41))
       self.pushButton.setObjectName("pushButton")
       self.widget = QtWidgets.QWidget(self.centralwidget)
       self.widget.setGeometry(QtCore.QRect(90, 160, 591, 341))
       self.widget.setObjectName("widget")
       self.label_3 = QtWidgets.QLabel(self.centralwidget)
       self.label_3.setGeometry(QtCore.QRect(50, 80, 121, 21))
       self.label_3.setObjectName("label_3")
       self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
       self.lineEdit.setGeometry(QtCore.QRect(180, 80, 113, 22))
       self.lineEdit.setObjectName("lineEdit")
       self.pushButton_2 = QtWidgets.QPushButton(self.centralwidget)
       self.pushButton_2.setGeometry(QtCore.QRect(690, 130, 81, 21))
       self.pushButton_2.setObjectName("pushButton_2")
       self.pushButton_3 = QtWidgets.QPushButton(self.centralwidget)
       self.pushButton_3.setGeometry(QtCore.QRect(690, 500, 91, 41))
       self.pushButton_3.setObjectName("pushButton_3")
       self.label_2 = QtWidgets.QLabel(self.centralwidget)
       self.label_2.setGeometry(QtCore.QRect(50, 110, 141, 16))
       self.label_2.setObjectName("label_2")
       self.lineEdit_2 = QtWidgets.QLineEdit(self.centralwidget)
       self.lineEdit_2.setGeometry(QtCore.QRect(200, 110, 113, 22))
       self.lineEdit_2.setObjectName("lineEdit_2")
       self.lineEdit_3 = QtWidgets.QLineEdit(self.centralwidget)
       self.lineEdit_3.setGeometry(QtCore.QRect(160, 50, 113, 22))
       self.lineEdit_3.setObjectName("lineEdit_3")
       self.label_4 = QtWidgets.QLabel(self.centralwidget)
       self.label_4.setGeometry(QtCore.QRect(50, 50, 111, 16))
       self.label_4.setObjectName("label_4")
       self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
       self.textBrowser.setGeometry(QtCore.QRect(460, 70, 221, 41))
       self.textBrowser.setObjectName("textBrowser")
       self.label_5 = QtWidgets.QLabel(self.centralwidget)
       self.label_5.setGeometry(QtCore.QRect(340, 80, 111, 16))
       self.label_5.setObjectName("label_5")
       MainWindow.setCentralWidget(self.centralwidget)
       self.menubar = QtWidgets.QMenuBar(MainWindow)
       self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 26))
       self.menubar.setObjectName("menubar")
       MainWindow.setMenuBar(self.menubar)
       self.statusbar = QtWidgets.QStatusBar(MainWindow)
       self.statusbar.setObjectName("statusbar")
       MainWindow.setStatusBar(self.statusbar)

       self.retranslateUi(MainWindow)
       self.pushButton_2.clicked.connect(self.lineEdit.clear)
       self.pushButton_2.clicked.connect(self.widget.close)
       self.pushButton_2.clicked.connect(self.lineEdit_2.clear)
       self.pushButton_2.clicked.connect(self.lineEdit_3.clear)
       self.pushButton_2.clicked.connect(self.textBrowser.clear)
       self.pushButton.clicked.connect(self.gauss)
       QtCore.QMetaObject.connectSlotsByName(MainWindow)
     
       self.lay = QHBoxLayout()
       self.figure = plt.figure()
       self.canvas = FigureCanvas(self.figure)
       self.lay.addWidget(self.canvas)
       self.widget.setLayout(self.lay)
       self.widget.setFixedWidth(591)
       self.widget.setFixedHeight(341)

       
   def gauss(self):
       import numpy as np
       import sys     
       import pandas as pd
       dataset = pd.read_csv(self.lineEdit_3.text())
       x = dataset.iloc[0:21].values
       y = dataset.iloc[22:43].values
      
         
       n = int(self.lineEdit_2.text())
       X = np.zeros(n)
       a = np.zeros((n,n))
       b = np.zeros(n)
       z = np.zeros(n+2)
       z[0]=int(self.lineEdit.text())
       

       for j in range(1,n+2):
           for i in range(1,21):
               z[j]+=np.power(float(x[i]),j)
              # print(z[j])

       for j in range(0,n):    
           for r in range(n):
               a[r][j]=z[j+r]


      # for i in range(0,n):
       #    for j in range(0,n):
               #print(a[i][j])
           
       for i in range(n):
           if a[i][i] == 0.0:
               sys.exit('Divide by zero detected!')
                   
           for j in range(i+1, n):
               ratio = a[j][i]/a[i][i]
               b[j]= b[j]- ratio* b[i]

               for k in range(n):
                   a[j][k] = a[j][k] - ratio * a[i][k]    
               
       #for i in range(n):
          # for j in range(n):
              #print(a[i][j])
                   
       #for i in range(n):
          # print(b[i])
                         
                  
           # Back Substitution
       X[n-1] = b[n-1]/a[n-1][n-1]

       for i in range(n-2,-1,-1):
           Sum = 0

           for j in range(i,n):
               Sum = Sum + a[i][j]*X[j]
               
           X[i] = (b[i] -Sum)/a[i][i]

            
       self.textBrowser.setText(str(X))
           
       plt.grid()
          # x = np.linspace(0,1,n)
          # for i in matrix(x,y):
             #  plt.plot(i[0], i[1], 'b')
           
           
           
       plt.xlabel("X-axis")
       plt.ylabel("Y-axis")
       plt.title("Polynomial Regression")
       #visualize data
       plt.plot(x,y, 'r')
       plt.axis('auto')
       plt.xlim((-0.05, 1.05))
       plt.ylim((-0.10, 0.10))
       self.figure.tight_layout()
       self.canvas.draw()

 

 
       
       
   def retranslateUi(self, MainWindow):
       _translate = QtCore.QCoreApplication.translate
       MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
       self.label.setText(_translate("MainWindow", "Polynomial Regression Method"))
       self.pushButton.setText(_translate("MainWindow", "Calculate"))
       self.label_3.setText(_translate("MainWindow", "Number of data, N :"))
       self.pushButton_2.setText(_translate("MainWindow", "Clear"))
       self.pushButton_3.setText(_translate("MainWindow", "Save file"))
       self.label_2.setText(_translate("MainWindow", "Number of unknown, m :"))
       self.label_4.setText(_translate("MainWindow", "Enter file name :"))


if _name_ == "_main_":
   app = QtWidgets.QApplication(sys.argv)
   MainWindow = QtWidgets.QMainWindow()
   ui = Ui_MainWindow()
   ui.setupUi(MainWindow)
   MainWindow.show()
   sys.exit(app.exec_())


What I have tried:

this is a python question .
i need step by step to create PyQt5 that can import CSV , user input function plot graph, save filename after into excel or text with graph get from answer get the answer and calculate the coefficient .
Posted
Updated 3-Jul-22 19:37pm
v2
Comments
Richard MacCutchan 4-Jul-22 6:56am    
" seems like got some error"
If you got errors, and you want help, then you must show us the errors, and explain where they occur. We cannot guess what happens when you run your code.
Eyaa Rajandran 4-Jul-22 7:05am    
I already mentioned up there . I couldn't get value for x and can't plot graph on GUI
Richard MacCutchan 4-Jul-22 7:21am    
Sorry, that tells us nothing. Please provide proper details of your problem.
Eyaa Rajandran 4-Jul-22 7:24am    
that is polynomial system and gauss elimination python coding. the coefficient which is x should be plotted on GUI, the problem is I couldn't get the value x.
The format is Ax = b, where matrix A and vector b .
Richard MacCutchan 4-Jul-22 7:28am    
"I couldn't get the value x."
Why not?

1 solution

Getting your code to run does not mean your code is right! :laugh:
Think of the development process as writing an email: compiling successfully means that you wrote the email in the right language - English, rather than German for example - not that the email contained the message you wanted to send.

So now you enter the second stage of development (in reality it's the fourth or fifth, but you'll come to the earlier stages later): Testing and Debugging.

Start by looking at what it does do, and how that differs from what you wanted. This is important, because it give you information as to why it's doing it. For example, if a program is intended to let the user enter a number and it doubles it and prints the answer, then if the input / output was like this:
Input   Expected output    Actual output
  1            2                 1
  2            4                 4
  3            6                 9
  4            8                16
Then it's fairly obvious that the problem is with the bit which doubles it - it's not adding itself to itself, or multiplying it by 2, it's multiplying it by itself and returning the square of the input.
So with that, you can look at the code and it's obvious that it's somewhere here:
C#
int Double(int value)
   {
   return value * value;
   }

Once you have an idea what might be going wrong, start using the debugger to find out why. Put a breakpoint on the first line of the method, and run your app. When it reaches the breakpoint, the debugger will stop, and hand control over to you. You can now run your code line-by-line (called "single stepping") and look at (or even change) variable contents as necessary (heck, you can even change the code and try again if you need to).
Think about what each line in the code should do before you execute it, and compare that to what it actually did when you use the "Step over" button to execute each line in turn. Did it do what you expect? If so, move on to the next line.
If not, why not? How does it differ?
Hopefully, that should help you locate which part of that code has a problem, and what the problem is.
This is a skill, and it's one which is well worth developing as it helps you in the real world as well as in development. And like all skills, it only improves by use!
 
Share this answer
 
Comments
Eyaa Rajandran 4-Jul-22 7:25am    
it is python coding
OriginalGriff 4-Jul-22 8:17am    
Yes. I know.
The principle is still exactly the same ...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900