0% found this document useful (0 votes)
82 views6 pages

Mathcad Matlab

The document demonstrates using MATLAB to solve differential equations numerically and analytically. It defines functions for the velocity and position of a falling object. It then uses these functions with numerical methods like Euler's method and the built-in ode45 solver. It also finds an analytical solution by taking the integral of the velocity function. Plots of the numerical and analytical solutions are shown.

Uploaded by

Cindy Feng
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
82 views6 pages

Mathcad Matlab

The document demonstrates using MATLAB to solve differential equations numerically and analytically. It defines functions for the velocity and position of a falling object. It then uses these functions with numerical methods like Euler's method and the built-in ode45 solver. It also finds an analytical solution by taking the integral of the velocity function. Plots of the numerical and analytical solutions are shown.

Uploaded by

Cindy Feng
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

mathcad-matlab.

mcd

>> cd

v0

Page 1

c:\

>> global v0
>> v0 = 40

40

[File] [New] [M-File]

function yp = yprime(t)
global v0
y' ( t )

v0

98 .
t
10

yp = v0 - 9.8*t;
[File] [Save] yprime.m

y' ( 0 ) = 40

y0

>> yprime(0)

>> global y0
>> y0 = 0

[File] [New] [M-File]

function yval = y(t)


global y0
for i=1:length(t)
t
y( t )

y' ( ) d

y0
0

yval(i) = y0 + ...
quad8('yprime',0,t(i));
end
[File] [Save] y.m

y ( 1 ) = 35.1

>>

y(1)

ist.uwaterloo.ca/
ew/saw/mathcad

mathcad-matlab.mcd

0 , 0.1 .. 10

>> t = [0 : 0.1 : 10];

>> plot(t, y(t), 'r-')

y( t )

>> hold on
>> plot(t, yprime(t), 'k-')
>> hold off

y'( t )

t, t

10

<-- Initial guess for t

>> t = 10;

Given
y( t ) 0
t

Page 2

Find ( t )

>> t = fzero('y', t)

t = 8.163
y ( t ) = 5.354 . 10

>> y(t)

ist.uwaterloo.ca/
ew/saw/mathcad

mathcad-matlab.mcd

Page 3

[File] [New] [M-File]

function root = ...


newt(f,fprime,t, tol)
newt ( f , f' , t , tol )

while abs( feval(f,t) ) > tol

f ( t ) > tol

while

t = t - feval(f,t) / ...
feval(fprime,t);
end

f( t )

f' ( t )

root = t;

[File] [Save] newt.m

10

newt y , y' , t , 10

>> t = 10;
5

>> t = newt('y','yprime',t,1e-5)

t = 8.163
y( t ) =

5.479 . 10

10

>> y(t)

t
y( t )

y' ( ) d

y0

40 . t

Symbolic solutions in
Mathcad and MATLAB
via built-in Maple kernel

Given
y( t ) 0
t

Find ( t )

49 . 2
t
10

400
49

T
t =

0
8.163

ist.uwaterloo.ca/
ew/saw/mathcad

y( 1 )

351
10

y ( 1 ) = 35.1

mathcad-matlab.mcd

Page 4

[File] [New] [M-File]

function yp = yprime2(t,y)
global v0
y' ( t , y )

98 .
t
10

v0

yp = v0 - 9.8*t;
[File] [Save] yprime2.m

Reference:D:\DATA\mathcad\saw\system.mcd(R)
t
D( t , y )
y

range ( 0 , 0.1 , 10 )

>> t = [0 : 0.1 : 10];

system ( t , y , y' , 1 )
solution ( t , y0 , D , 1 )

>> [t,y] = ode45('yprime2',t,y0);

100

10

>> subplot(2,1,1)
>> plot(t, y, 'r-')

100
t
100

100

50

50

>> subplot(2,1,2)
>> plot(yprime2(t,y), y, 'r-')

100
y'( t , y )

ist.uwaterloo.ca/
ew/saw/mathcad

mathcad-matlab.mcd

euler ( t0 , y0 , f' , tend , n )

tend
n

t1

t0

y1

y0

Page 5

t0
1

for i 2 .. n
ti

ti

yi

yi

t . f'

ti

1 , yi

t
y
[File] [New] [M-File]

function [t,y] = ...


euler(t0,y0,fprime,tend,n)
delta_t = (tend - t0)/(n - 1);
t(1) = t0;
y(1) = y0;
for i=2:n
t(i) = t(i-1) + delta_t;
y(i) = y(i-1) + delta_t * ...
feval(fprime,t(i-1),y(i-1));
end
[File] [Save] euler.m

t
y

euler ( 0 , y0 , y' , 10 , 100 )

[t,y] = ...
euler(0,y0,'yprime2',10,100);

ist.uwaterloo.ca/
ew/saw/mathcad

mathcad-matlab.mcd

Page 6

100
50

>> plot(t, y, 'r-')

10

50
100
t

40

30

>> t = [0 1 2 3 4]';

20

>> v = [40 30 20 10 0]';

2
3

10

0
>> ba = polyfit(t,v,1)

slope ( t , v )

a =

intercept ( t , v )

b = 40

ORIGIN
< 1>

10
>> b = ba(1);
>> a = ba(2);

1
t

< 2>

>> M = [t.^0

t.^1]

1 0
1 1
M =

1 2
1 3
1 4

T
M .M

a
a =

10

. MT . v

b = 40

ist.uwaterloo.ca/
ew/saw/mathcad

>> ba = inv(M'*M)*(M'*v)
>> b = ba(1);
>> a = ba(2);

You might also like