Convert MATLAB Code to Python Code (.ipynb)

24 vues (au cours des 30 derniers jours)
Mujtaba Farrukh
Mujtaba Farrukh le 12 Mai 2022
Objective : Conversion of MATLAB code to Python Code
Hi,
I want to convert a really small and short code of MATLAB (.m) to Python (.ipynb).
Following is the code:
%% CLEARING THE PARAMETERS
clc;
clear;
close all;
%% PROGRAM
N=500;
M=double(diag(ones(1,N-1),1) | diag(ones(1,N-1),-1));
v=ones(1,N)*-2;
n = size(M,1);
M(1:(n+1):end) = v;
vec=inv(M)*ones(N,1);
x=1/(N+1):1/(N+1):1-1/(N+1);
plot(x,vec,'r','LineWidth',3)
xlabel('x values')
ylabel('vector')
grid minor
axis tight
Please can anyone tell me how to do it?

Réponse acceptée

Al Danial
Al Danial le 21 Mai 2022
#!/usr/bin/env python
import numpy as np
import matplotlib.pyplot as plt
N = 500
M = np.diag(np.ones((N-1,)),1) + np.diag(np.ones((N-1,)),-1)
v = np.ones((N,))*-2
M += np.diag(v)
vec = np.linalg.inv(M).dot(np.ones((N,)))
x = np.linspace(1/(N+1), 1 - 1/(N+1), N)
plt.plot(x,vec,'r', linewidth=3)
plt.xlabel('x values')
plt.ylabel('vector')
plt.grid()
plt.savefig('parabola.png', bbox_inches='tight', pad_inches=0.1)
plt.show()

Plus de réponses (0)

Catégories

En savoir plus sur Call Python from MATLAB dans Help Center et File Exchange

Produits


Version

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by