Info

此问题已关闭。 请重新打开它进行编辑或回答。

facing problem to function calling

2 次查看(过去 30 天)
suketu vaidya
suketu vaidya 2020-11-9
关闭: MATLAB Answer Bot 2021-8-20
function [x,y1]=exlicit(f1)
h =1;
x = -pi:h:pi;
n = 0:1:10;
y1 = [0];
for i=1:n
x(i+1)=x(i)+h;
y1(i+1)=y1(i)+h*f1(x(i),y1(i));
end
end
%heun's method
function [x,y1]=heun(f1)
h =1;
x = -pi:h:pi;
n = 0:1:10;
y1 = [0];
for i=1:n
x(i+1)=x(i)+h;
ynew=y1(i)+h*(f1(x(i),y1(i)));
y1(i+1)=y1(i)+(h/2)*(f1(x(i+1),y1(i))+h*f1(x(i+1),ynew));
end
end
%euler implicit method
function [x,y1]=implicit(f1)
h =1;
x = -pi:h:pi;
n = 0:1:10;
y1 = [0];
for i=1:n
x(i+1)=x(i)+h;
ynew=y1(i)+h*(f1(x(i),y1(i)));
y1(i+1)=y1(i)+h*f1(x(i+1),ynew);
end
end
%Runge Kutta 4th order method:
function [x,y1]=Runge(f1)
h =1;
x = -pi:h:pi;
n = 0:1:10;
y1 = [0];
for i=1:n
k1=f1(x(i),y1(i));
y1(i+1)=y1(i)+(h*k1)
end
end
function dy =f1(x,y1)
d=50;
c1=-1-d^2/(d^2+1);
x=0:0.01:10;
dy=c1*exp(-d*x)+d*sin(x)/(d^2+1)+d^2*cos(x)/(d^2+1);
end
%plot
%call function
[x2,y2]=exlicit(f1);
[x3,y3]=heun(f1);
[x4,y4]=implicit(f1);
[x5,y5]=Runge(f1);
plot(x2,y2,'g-',x3,y3,'b',x4,y4,'m-',x5,y5,'r')
hold on
end
  3 个评论
suketu vaidya
suketu vaidya 2020-11-9
yes sir ,
Plot numerical solutions of the problem obtained with explicit Euler, implicit Euler, Heun and RK4 methods. Plot all numerical solutions on a single figure together with analytical one
Rik
Rik 2020-11-9
Well, you will first have to fix what is inside a function and what is outside of it. Pay attention to m-lint: those squiggly lines under your code should all be gone. It will give you advice how to solve them.

回答(0 个)

此问题已关闭。

Community Treasure Hunt

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

Start Hunting!

Translated by