| 8 | 1/1 | 返回列表 |
| 查看: 2030 | 回復(fù): 7 | |||
longwen8木蟲 (文壇精英)
|
[求助]
微分方程組的四階龍格庫塔公式求解matlab版 已有1人參與
|
|
微分方程組的四階龍格庫塔公式,求解matlab版的代碼。下面是我的代碼,運行不出來。 function varargout=rungekutta(varargin) clc,clear x0=0;xn=1.0;y0=1;h=0.05;%h為步長 [y,x]=rungekutta4(x0,xn,y0,h); Function z=f(x,y); z=y-2*x/y; function [y,x]=rungekutta4(x0,xn,y0,h) x=x0:h:xn; n=(xn-x0)/h; y1=x; y1(1)=y0; for i=1:n %龍格庫塔法的算法 K1=f(x(i),y1(i)); K2=f(x(i)+h/2,y1(i)+h/2*K1); K3= f(x(i)+h/2,y1(i)+h/2*K2); K4= f(x(i)+h,y1(i)+h*K3); y1(i+1)=y1(i)+h/6*(K1+2*K2+2*K3+K4); yy1(i)=(1+2*x(i+1)) ^0.5; error(i)=y1(i+1)-yy1(i); yy(i+1)=yy1(i); e(i+1)=error(i); end y=y1; n=(xn-x0)/h; fprintf(‘i x(i) yi數(shù)值解 y(i)真值 誤差\n’); fprintf(‘-------------------------------------------------------------\n’); for i=1:n fprintf(‘%2d %12.4f %14.8f %14.8f %12.8f\n’,i, x(i+1), y(i+1), yy(i+1), error(i)); end plot(x,e,‘r.’);%畫出誤差曲線 xlable(‘x軸’),ylable(‘誤差’); title(‘步長為0.05時的誤差曲線’); |
|
一、幾個自定義函數(shù)位置錯誤。應(yīng)為這樣放置 1、主程序 , function rungekutta() clc,clear x0=0;xn=1.0;y0=1;h=0.05;%h為步長 [y,x]=rungekutta4(x0,xn,y0,h); end 2、 四階龍格庫塔函數(shù)程序 function [y,x]=rungekutta4(x0,xn,y0,h) 。。。 end 3、z函數(shù)程序 function z=f(x,y); z=y-2*x/y; end 二、xlable(‘x軸’),ylable(‘誤差’);這句代碼中函數(shù)書寫錯誤。應(yīng)為 xlabel('x軸'),ylabel('誤差'); 三、修改后運行得到如下結(jié)果 |

木蟲 (文壇精英)
|
運行結(jié)果: i x(i) yi數(shù)值解 y(i)真值 誤差 ------------------------------------------------------------- 1 0.0500 1.04880886 1.04880885 0.00000001 2 0.1000 1.09544514 1.09544512 0.00000002 3 0.1500 1.14017546 1.14017543 0.00000004 4 0.2000 1.18321600 1.18321596 0.00000005 5 0.2500 1.22474493 1.22474487 0.00000006 6 0.3000 1.26491113 1.26491106 0.00000007 7 0.3500 1.30384056 1.30384048 0.00000008 8 0.4000 1.34164088 1.34164079 0.00000009 9 0.4500 1.37840498 1.37840488 0.00000011 10 0.5000 1.41421368 1.41421356 0.00000012 11 0.5500 1.44913781 1.44913767 0.00000014 12 0.6000 1.48323985 1.48323970 0.00000015 13 0.6500 1.51657526 1.51657509 0.00000017 14 0.7000 1.54919353 1.54919334 0.00000019 15 0.7500 1.58113904 1.58113883 0.00000021 16 0.8000 1.61245178 1.61245155 0.00000023 17 0.8500 1.64316793 1.64316767 0.00000026 18 0.9000 1.67332034 1.67332005 0.00000028 19 0.9500 1.70293895 1.70293864 0.00000031 20 1.0000 1.73205115 1.73205081 0.00000034 圖像上傳不上來。 |

木蟲 (文壇精英)
木蟲 (文壇精英)
|
function rungekutta() clc,clear x0=0;xn=1.0;y0=1;h=0.05;%h為步長 [y,x]=rungekutta4(x0,xn,y0,h); End function [y,x]=rungekutta4(x0,xn,y0,h) x=x0:h:xn; n=(xn-x0)/h; y1=x; y1(1)=y0; for i=1:n %龍格庫塔法的算法 K1=f(x(i),y1(i)); K2=f(x(i)+h/2,y1(i)+h/2*K1); K3= f(x(i)+h/2,y1(i)+h/2*K2); K4= f(x(i)+h,y1(i)+h*K3); y1(i+1)=y1(i)+h/6*(K1+2*K2+2*K3+K4); yy1(i)=(1+2*x(i+1))^0.5; error(i)=y1(i+1)-yy1(i); yy(i+1)=yy1(i); e(i+1)=error(i); end function z=f(x,y); z=y-2*x/y; end y=y1; n=(xn-x0)/h; fprintf('i x(i) yi數(shù)值解 y(i)真值 誤差\n'); fprintf('-------------------------------------------------------------\n'); for i=1:n fprintf(‘%2d %12.4f %14.8f %14.8f %12.8f\n’,i, x(i+1), y(i+1), yy(i+1), error(i)); end plot(x,e, 'r. ');%畫出誤差曲線 xlabel('x軸'),ylabel('誤差'); title('步長為0.05時的誤差曲線'); 大神,我修改完,還是運行不出來啊。你能不能直接把你的完整代碼發(fā)給我,我自己找找錯誤。 |

木蟲 (文壇精英)
| 8 | 1/1 | 返回列表 |
| 最具人氣熱帖推薦 [查看全部] | 作者 | 回/看 | 最后發(fā)表 | |
|---|---|---|---|---|
|
[考研] 材料277分求調(diào)劑 +13 | 飯飯星球 2026-03-04 | 14/700 |
|
|---|---|---|---|---|
|
[考研] 求調(diào)劑 +5 | danyyyy 2026-03-04 | 5/250 |
|
|
[考研] 334求調(diào)劑 +6 | Trying] 2026-03-05 | 8/400 |
|
|
[考研] 一志愿武理085601專碩347分求調(diào)劑 +4 | 啊歐歐歐 2026-03-04 | 5/250 |
|
|
[考研] 264求調(diào)劑 +8 | 26調(diào)劑 2026-03-03 | 8/400 |
|
|
[考研] 267化工調(diào)劑求助 +5 | 聰少OZ 2026-03-04 | 5/250 |
|
|
[考研] 295求調(diào)劑 +6 | 等春來, 2026-03-04 | 6/300 |
|
|
[考研] 085600求調(diào)劑 +4 | LRZZZZZZ 2026-03-02 | 6/300 |
|
|
[考研] 材料工程269求調(diào)劑 +7 | 白刺玫 2026-03-02 | 7/350 |
|
|
[考研]
|
旅行中的紫葡萄 2026-03-03 | 4/200 |
|
|
[考研] 環(huán)境調(diào)劑 +8 | chenhanheng 2026-03-02 | 8/400 |
|
|
[考研] 0856材料工程,初試313調(diào)劑 +7 | 賣個關(guān)子吧 2026-03-03 | 7/350 |
|
|
[考研] 290求調(diào)劑 +9 | ErMiao1020 2026-03-02 | 9/450 |
|
|
[考研] 288求調(diào)劑 +3 | 少71.8 2026-03-02 | 5/250 |
|
|
[考研] 求調(diào)劑 +11 | yunziaaaaa 2026-03-01 | 13/650 |
|
|
[考研] 0856求調(diào)劑285 +11 | 呂仔龍 2026-02-28 | 11/550 |
|
|
[考研] 材料工程274求調(diào)劑 +5 | Lilithan 2026-03-01 | 5/250 |
|
|
[考研] 材料調(diào)劑 +6 | 愛擦汗的可樂冰 2026-02-28 | 7/350 |
|
|
[考研]
|
LYidhsjabdj 2026-02-28 | 4/200 |
|
|
[考研] 307求調(diào)劑 +4 | 73372112 2026-02-28 | 6/300 |
|