انجمن سایت کلیدستان


رتبه موضوع:
  • 1 رای - 5 میانگین
  • 1
  • 2
  • 3
  • 4
  • 5
معادله جبری خطی با n مجهول

معادله جبری خطی با n مجهول

#5
من کدها را به گونه ای نوشته ام که بتوانید با تعیین n دلخواه و سپس تعیین ثابت های متناظر با آن، کل فرآیند را به صورت خودکار توسط متلب شبیه سازی کنید، هم معادلات با ضرایب آن به شما نشان داده می شود و هم نتایج را در آخر برنامه خواهید دید (یعنی مقدار لانداها).
در ابتدای کدها، ثابت های لازم برای حالت n برابر 2 و n برابر 3 را نوشته ام که مثلا فعلا حالت n برابر 2 را می خواهیم اجرا کنیم و بنابراین ثابت های نوشته شده برای حالت n برابر 3 را به صورت توضیح در می آورم. 
اینکه فعلا n را برابر 2 می گیرم برای این است که هم به طور ساده روند کلی برنامه را متوجه بشوید و هم خودتان برنامه را با دقت چک کنید که مبادا جایی اشتباه کرده باشم، بعد می توانید حالت n برابر 8 را اجرا کنید (با تعیین n برابر 8 و سپس تعریف ثابت های مورد نیاز).
برنامه ممکن است کمی پیچیده به نظر بیاید، بنابراین توضیحاتی را در میان همان کدها نوشته ام، هر دستوری را که متوجه نشدید، به مباحث سایت مراجعه کنید و درباره آن بخوانید.
خوب فعلا برنامه را برای حالت n برابر 2 اجرا می کنیم، اینم کدهای متلب :

کد:
clear all
close all
clc

% define constants
n=2;
V=2;
beta_i=[0.5 1];
I_ij=[1 2;3 4];

% n=3;
% V=2;
% beta_i=[0.5 1 0.7];
% I_ij=[1 2 3; 4 5 6; 7 8 9];


% define symbolic variables
for k=1:n
    command_string=strcat('syms landa_' , num2str(k) ,';'); % strcat command is for concatenate multiple strings --- num2str command is for convert number to string
    eval(command_string); % eval command receive an string and execute it like a line of command
end
whos % to see that landa variables are defined in matlab

% define equations
equation=cell(n,1); % create an cell array for equations --> each cell will be an equation (string)
for i=1:n
    string='';
    for j=1:n
        if(j==i)
            coefficient=0.5; % variable coefficient
            string=strcat(string,num2str(coefficient),'*','landa_',num2str(j),'+'); % strcat command is for concatenate multiple strings
        else
            coefficient=I_ij(i,j)/(2*pi); % variable coefficient
            string=strcat(string,num2str(coefficient),'*','landa_',num2str(j),'+'); % strcat command is for concatenate multiple strings
        end
    end
    value=V*cos(beta_i(i));
    equation{i}=strcat(string,num2str(value),'=0');
end

% display equations in output
disp('---------------------') % disp command will display an message in output
for i=1:n
    message=strcat('equation number ((',num2str(i),')) : ');
    disp(message) % disp command will display an message in output
    disp(equation{i})
    disp('---------------------')
end

% solve the equations
solve_command_string='S=solve(';
for k=1:n
    if(k==n)
        solve_command_string=strcat(solve_command_string,'''',equation{k},'''');
    else
        solve_command_string=strcat(solve_command_string,'''',equation{k},'''',',');
    end
end
solve_command_string=strcat(solve_command_string,')');
solve_command_string
eval(solve_command_string);

% show results
command_string='S=[';
for k=1:n
    if(k==n)
        command_string=strcat(command_string,'S.landa_',num2str(k));
    else
        command_string=strcat(command_string,'S.landa_',num2str(k),',');
    end
end
command_string=strcat(command_string,']');
command_string
eval(command_string);

disp('---------------------')
for k=1:n
    message=strcat('landa_',num2str(k),' : ');
    disp(message) % disp command will display an message in output
    disp(S(k))
    disp('---------------------')
end

و نتیجه به صورت زیر است :

کد:
Name                Size            Bytes  Class     Attributes

  I_ij                2x2                32  double              
  V                   1x1                 8  double              
  beta_i              1x2                16  double              
  command_string      1x13               26  char                
  k                   1x1                 8  double              
  landa_1             1x1               112  sym                
  landa_2             1x1               112  sym                
  n                   1x1                 8  double              

---------------------
equation number ((1)) :
0.5*landa_1+0.31831*landa_2+1.7552=0
---------------------
equation number ((2)) :
0.47746*landa_1+0.5*landa_2+1.0806=0
---------------------

solve_command_string =

S=solve('0.5*landa_1+0.31831*landa_2+1.7552=0','0.47746*landa_1+0.5*landa_2+1.0806=0')


S =

    landa_1: [1x1 sym]
    landa_2: [1x1 sym]


command_string =

S=[S.landa_1,S.landa_2]


S =

[ -5.4441522848292036423687589991724, 3.0375298998291031421707753434898]

---------------------
landa_1 :
-5.4441522848292036423687589991724

---------------------
landa_2 :
3.0375298998291031421707753434898

---------------------

دقت کنید که با دستور whos ، ابتدا متغیرهای تعریف شده در متلب را نمایش داده ایم تا ببینید که لانداها به صورت سمبلیک تعریف شده اند.

خوب حالا برنامه را برای حالت n برابر 3 اجرا می کنیم :

کد:
clear all
close all
clc

% define constants
% n=2;
% V=2;
% beta_i=[0.5 1];
% I_ij=[1 2;3 4];

n=3;
V=2;
beta_i=[0.5 1 0.7];
I_ij=[1 2 3; 4 5 6; 7 8 9];


% define symbolic variables
for k=1:n
    command_string=strcat('syms landa_' , num2str(k) ,';'); % strcat command is for concatenate multiple strings --- num2str command is for convert number to string
    eval(command_string); % eval command receive an string and execute it like a line of command
end
whos % to see that landa variables are defined in matlab

% define equations
equation=cell(n,1); % create an cell array for equations --> each cell will be an equation (string)
for i=1:n
    string='';
    for j=1:n
        if(j==i)
            coefficient=0.5; % variable coefficient
            string=strcat(string,num2str(coefficient),'*','landa_',num2str(j),'+'); % strcat command is for concatenate multiple strings
        else
            coefficient=I_ij(i,j)/(2*pi); % variable coefficient
            string=strcat(string,num2str(coefficient),'*','landa_',num2str(j),'+'); % strcat command is for concatenate multiple strings
        end
    end
    value=V*cos(beta_i(i));
    equation{i}=strcat(string,num2str(value),'=0');
end

% display equations in output
disp('---------------------') % disp command will display an message in output
for i=1:n
    message=strcat('equation number ((',num2str(i),')) : ');
    disp(message) % disp command will display an message in output
    disp(equation{i})
    disp('---------------------')
end

% solve the equations
solve_command_string='S=solve(';
for k=1:n
    if(k==n)
        solve_command_string=strcat(solve_command_string,'''',equation{k},'''');
    else
        solve_command_string=strcat(solve_command_string,'''',equation{k},'''',',');
    end
end
solve_command_string=strcat(solve_command_string,')');
solve_command_string
eval(solve_command_string);

% show results
command_string='S=[';
for k=1:n
    if(k==n)
        command_string=strcat(command_string,'S.landa_',num2str(k));
    else
        command_string=strcat(command_string,'S.landa_',num2str(k),',');
    end
end
command_string=strcat(command_string,']');
command_string
eval(command_string);

disp('---------------------')
for k=1:n
    message=strcat('landa_',num2str(k),' : ');
    disp(message) % disp command will display an message in output
    disp(S(k))
    disp('---------------------')
end

و نتیجه اجرای آن به صورت زیر است :

کد:
Name                Size            Bytes  Class     Attributes

  I_ij                3x3                72  double              
  V                   1x1                 8  double              
  beta_i              1x3                24  double              
  command_string      1x13               26  char                
  k                   1x1                 8  double              
  landa_1             1x1               112  sym                
  landa_2             1x1               112  sym                
  landa_3             1x1               112  sym                
  n                   1x1                 8  double              

---------------------
equation number ((1)) :
0.5*landa_1+0.31831*landa_2+0.47746*landa_3+1.7552=0
---------------------
equation number ((2)) :
0.63662*landa_1+0.5*landa_2+0.95493*landa_3+1.0806=0
---------------------
equation number ((3)) :
1.1141*landa_1+1.2732*landa_2+0.5*landa_3+1.5297=0
---------------------

solve_command_string =

S=solve('0.5*landa_1+0.31831*landa_2+0.47746*landa_3+1.7552=0','0.63662*landa_1+0.5*landa_2+0.95493*landa_3+1.0806=0','1.1141*landa_1+1.2732*landa_2+0.5*landa_3+1.5297=0')


S =

    landa_1: [1x1 sym]
    landa_2: [1x1 sym]
    landa_3: [1x1 sym]


command_string =

S=[S.landa_1,S.landa_2,S.landa_3]


S =

[ -8.9160689597352526983972784600161, 5.9298654248571101229106934347949, 1.7075755382259448455890261022463]

---------------------
landa_1 :
-8.9160689597352526983972784600161

---------------------
landa_2 :
5.9298654248571101229106934347949

---------------------
landa_3 :
1.7075755382259448455890261022463

---------------------

فقط دقت کنید که در کدها، cos داشتیم، چک کنید ببینید که واحد مقادیری که باید به آن بدهید چیست (زاویه یا رادیان) و سپس به آنها مقدار بدهید، زیرا گاهی افراد به این نکته توجهی ندارند و نتایج غلط به دست می آورند.

فردا شب عیده و سال جدید شروع میشه، سال نوتون پیشاپیش مبارک. 
bookbook 
لطفا برای درج کد، از دکمه مخصوص درج کد در ادیتور انجمن استفاده کنید.
در مورد برنامه نویسی، مدیران تنها راهنمایی می کنند و نوشتن برنامه نهایی، به عهده کاربران می باشد (اینجا محلی برای یادگیری است، نه سفارش کدنویسی).
کاربران باید ابتدا خود به خطایابی برنامه بپردازند، نه اینکه به محض دیدن خطا، کدها را در انجمن، copy و paste کرده و از مدیران انتظار بررسی داشته باشند.
پاسخ


پیام‌های این موضوع
معادله جبری خطی با n مجهول - توسط tip - ۱۳۹۲/۱۲/۲۷, ۰۳:۳۳ ب.ظ
RE: معادله جبری خطی با n مجهول - توسط admin - ۱۳۹۲/۱۲/۲۷, ۰۳:۴۸ ب.ظ
RE: معادله جبری خطی با n مجهول - توسط tip - ۱۳۹۲/۱۲/۲۷, ۰۴:۰۶ ب.ظ
RE: معادله جبری خطی با n مجهول - توسط admin - ۱۳۹۲/۱۲/۲۷, ۰۵:۲۰ ب.ظ
RE: معادله جبری خطی با n مجهول - توسط admin - ۱۳۹۲/۱۲/۲۸, ۱۱:۱۸ ق.ظ
RE: معادله جبری خطی با n مجهول - توسط tip - ۱۳۹۲/۱۲/۲۸, ۰۴:۰۷ ب.ظ
RE: معادله جبری خطی با n مجهول - توسط admin - ۱۳۹۲/۱۲/۲۸, ۰۶:۲۵ ب.ظ
RE: معادله جبری خطی با n مجهول - توسط tip - ۱۳۹۲/۱۲/۲۸, ۰۸:۴۰ ب.ظ
RE: معادله جبری خطی با n مجهول - توسط admin - ۱۳۹۲/۱۲/۲۸, ۰۹:۰۱ ب.ظ

پرش به انجمن:


کاربران در حال بازدید این موضوع: 2 مهمان