Info

This question is closed. Reopen it to edit or answer.

facing problem to function calling

1 view (last 30 days)
suketu vaidya
suketu vaidya on 9 Nov 2020
Closed: MATLAB Answer Bot on 20 Aug 2021
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 Comments
Rik
Rik on 9 Nov 2020
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.

Answers (0)

Community Treasure Hunt

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

Start Hunting!