Convert MATLAB Code to Python Code (.ipynb)

조회 수: 25 (최근 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 CenterFile Exchange에서 Call 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