Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Midpoint Method

Subject: Midpoint Method

From: faizal

Date: 3 Dec, 2012 12:28:07

Message: 1 of 2

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

Subject: Midpoint Method

From: Kristin

Date: 7 Dec, 2012 13:54:08

Message: 2 of 2

"faizal " <bb_zal@yahoo.com> wrote in message <k9i5sn$atc$1@newscl01ah.mathworks.com>...
>
> fcnstr='x^2 - 3*x^3 + 0.5*x^4' ;
>
> %Converting the input function to that can be used
> f=inline(fcnstr) ;
>
> "??? 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

Look at fcnstr. Notice that it only has x's as variables, so when you create the inline function, there is only one variable, so only 1 argument.

Somewhere after you make the function f, type "keyboard" and run your code. You will be stopped at this line. Then, in your command window, type "f", and you'll see what it looks like:

f =

     Inline function:
     f(x) = x^2 - 3*x^3 + 0.5*x^4

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us