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:
urgent help - ode45 and plotting

Subject: urgent help - ode45 and plotting

From: Sarah Coleman

Date: 19 Apr, 2012 06:32:30

Message: 1 of 2

Hi all, What I'm trying to do is plot 5 curves on 1 figure. The difference between the curves is by varying the constant "A".

Please let me where and what I've done wrong. I've 2 codes, the first is a function which calls the other function and the second is the "integrating" function.

Thank you.

"Main" function:

function Plotting_Of_Different_A_Values

tstart = 0;
tend = 10;
n = 10000;
tspan = linspace(tstart,tend,n);
xinit = [0;0];

beta = 55;
gamma = 0.2;
%g = 9.8;

A = [0.5,1,1.5,2,5];


options = odeset('RelTol', 1e-10, 'AbsTol', 1e-10);


for k = 1:length(A)
    
    %[t,x] = ode45(@(t,x)IntegratingFunction(t,x,beta,gamma,A(k)), tspan, xinit,options);
    [tLinear,y] = ode45(@(tLinear,y)IntegratingFunction2(tLinear,y,beta,gamma,A(k)), tspan, xinit,options);
    
    %z = x(:,1);
    
    
    zLinear = y(:,1);
    %driLinear = (beta^2/g)* max(zLinear);
    
     
    
    if A(k) == 0.5
        figure(1);
        plot(tLinear,zLinear,'r--');
        text(2,0.0447, 'A=0.5');
        text(2,0.21, 'A=0.5');
    elseif A(k) == 1
        plot(tLinear,zLinear,'g--');
        text(2,0.07, 'A=1');
        text(2,0.345, 'A=1');
    elseif A(k) == 1.5
        plot(tLinear,zLinear,'b--');
        text(1.5,0.07, 'A=1.5')
        text(2,0.4, 'A=1.5')
    elseif A(k) == 2
        plot(tLinear,zLinear,'m--');
        text(2.75,0.075, 'A=2')
        text(2,0.425, 'A=2')
    else
        plot(tLinear,zLinear,'k--');
        text(2.65,0.4125, 'A=5')
        text(1.15,0.075, 'A=5')
    end
        
    hold on
    xlabel('t(s)');
    ylabel('Z');

   
    
    
        
end




end


SECOND FUNCTION:


function [ dydt ] = IntegratingFunction2(tLinear,y,beta,gamma,A)
%UNTITLED2 Summary of this function goes here
% Detailed explanation goes here

z = y(1);
zdot = y(2);

dydt = zeros(size(y));
dydt(1) = zdot;
dydt(2) = heaviside(A*tLinear) + 4*heaviside(A*(tLinear-1)) - 5*heaviside(A*(tLinear-3)) - beta*gamma*zdot - beta*z;

return

Subject: urgent help - ode45 and plotting

From: Sarah Coleman

Date: 19 Apr, 2012 07:12:07

Message: 2 of 2

Bump

"Sarah Coleman" wrote in message <jmobhu$acm$1@newscl01ah.mathworks.com>...
> Hi all, What I'm trying to do is plot 5 curves on 1 figure. The difference between the curves is by varying the constant "A".
>
> Please let me where and what I've done wrong. I've 2 codes, the first is a function which calls the other function and the second is the "integrating" function.
>
> Thank you.
>
> "Main" function:
>
> function Plotting_Of_Different_A_Values
>
> tstart = 0;
> tend = 10;
> n = 10000;
> tspan = linspace(tstart,tend,n);
> xinit = [0;0];
>
> beta = 55;
> gamma = 0.2;
> %g = 9.8;
>
> A = [0.5,1,1.5,2,5];
>
>
> options = odeset('RelTol', 1e-10, 'AbsTol', 1e-10);
>
>
> for k = 1:length(A)
>
> %[t,x] = ode45(@(t,x)IntegratingFunction(t,x,beta,gamma,A(k)), tspan, xinit,options);
> [tLinear,y] = ode45(@(tLinear,y)IntegratingFunction2(tLinear,y,beta,gamma,A(k)), tspan, xinit,options);
>
> %z = x(:,1);
>
>
> zLinear = y(:,1);
> %driLinear = (beta^2/g)* max(zLinear);
>
>
>
> if A(k) == 0.5
> figure(1);
> plot(tLinear,zLinear,'r--');
> text(2,0.0447, 'A=0.5');
> text(2,0.21, 'A=0.5');
> elseif A(k) == 1
> plot(tLinear,zLinear,'g--');
> text(2,0.07, 'A=1');
> text(2,0.345, 'A=1');
> elseif A(k) == 1.5
> plot(tLinear,zLinear,'b--');
> text(1.5,0.07, 'A=1.5')
> text(2,0.4, 'A=1.5')
> elseif A(k) == 2
> plot(tLinear,zLinear,'m--');
> text(2.75,0.075, 'A=2')
> text(2,0.425, 'A=2')
> else
> plot(tLinear,zLinear,'k--');
> text(2.65,0.4125, 'A=5')
> text(1.15,0.075, 'A=5')
> end
>
> hold on
> xlabel('t(s)');
> ylabel('Z');
>
>
>
>
>
> end
>
>
>
>
> end
>
>
> SECOND FUNCTION:
>
>
> function [ dydt ] = IntegratingFunction2(tLinear,y,beta,gamma,A)
> %UNTITLED2 Summary of this function goes here
> % Detailed explanation goes here
>
> z = y(1);
> zdot = y(2);
>
> dydt = zeros(size(y));
> dydt(1) = zdot;
> dydt(2) = heaviside(A*tLinear) + 4*heaviside(A*(tLinear-1)) - 5*heaviside(A*(tLinear-3)) - beta*gamma*zdot - beta*z;
>
> return

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