Convert MATLAB Code to Python Code (.ipynb)

27 ビュー (過去 30 日間)
Mujtaba Farrukh
Mujtaba Farrukh 2022 年 5 月 12 日
回答済み: Al Danial 2022 年 5 月 21 日
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?

採用された回答

Al Danial
Al Danial 2022 年 5 月 21 日
#!/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()

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCall Python from MATLAB についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by