Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: Midpoint Method
Replies: 1   Last Post: Dec 7, 2012 8:54 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
faizal

Posts: 1
Registered: 12/3/12
Midpoint Method
Posted: Dec 3, 2012 7:28 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

clc
clear all
clc

fprintf('Comparing Exact Solution and Midpoint Method\n')

fcnstr='x^2 - 3*x^3 + 0.5*x^4' ;
% Initial value of x
x0=0;
% Initial value of y
y0=1;
% Final value of y
xf=4;
% number of steps to go from x0 to xf.
% This determines step size h=(xf-x0)/n
n=8;

%Converting the input function to that can be used
f=inline(fcnstr) ;

% Exact Solution
syms x
eqn=['Dy=' fcnstr]
% exact solution of the ode
exact_solution=dsolve(eqn,'y(0)=0','x')
% geting points for plotting the exact solution
xx=x0:(xf-x0)/100:xf;
yy=subs(exact_solution,x,xx);
yexact=subs(exact_solution,x,xf);
plot(xx,yy,'.')
hold on

%Midpoint Method
h=(xf-x0)/n;
a1=0;
a2=1;
p1=1/2;
q11=1/2;
%Initial values of x and y
xr(1)=x0;
yr(1)=y0;
for i=1:n
ka=f(xr(i),yr(i));
kb=f(xr(i)+p1*h,yr(i)+q11*ka*h);
yr(i+1)=yr(i)+(a1*ka+a2*kb)*h;
xr(i+1)=xr(i)+h;
end
%Value of y at x=xf
y_improved=yr(n+1);
% Absolute relative true error for value using Midpoint Method
et_improved=abs((y_improved-yexact)/yexact)*100;
hold on
plot(xr,yr,'color','red','LineWidth',2)


fprintf('\nAt x = %g ',xf)
disp(' ')
disp('_________________________________________________________________')
disp('Method Value Absolute Relative True Error')
disp('_________________________________________________________________')
fprintf('\nExact Solution %g',yexact)
fprintf('\nMidpoint method %g %g ',y_improved,et_improved)
disp( ' ')

this code i edited and try to run it like this,
but it keeps on showing
"??? Error using ==> inline.subsref at 17
Too many inputs to inline function.
Error in ==> Untitled18 at 47
ka=f(xr(i),yr(i));"
Mind to explain to me what the probelm here...i'm a newbie using Matlab


Date Subject Author
12/3/12
Read Midpoint Method
faizal
12/7/12
Read Re: Midpoint Method
Kristin

Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.