matlabmathnumerical-integrationcalculus

How to solve integral in Matlab having an upper limit set as the sine function?


I am getting error in executing the code below for calculating one simple integral in matlab. The code is given below.

%Parameter Innitizlation

epsilon0=8.85*10^-12;
d_mos=6*10^-9;
epsilon_mos=5*epsilon0;
d_g=30*10^-9;
epsilon_g=19*epsilon0;
vt=-2;
e=1.6*10^-19;
n=[];
i=1;
t2=[];
u=60; % cm^2/v*S
h=1.05*10^-34;  % ev*s


%Capacitor Calaculation
c_g=(epsilon_g/d_g);
c_mos=(epsilon_mos/d_mos);
c_t=1/((1/c_g)+(1/c_mos));


%Input Voltage 
t = 0:0.2:10;
vg = 5*sin(t);

%Surface Voltage
fun=1-(c_t/c_g);
vs = integral(fun,0,vg);

figure 
plot(t,vs)
title('vs vs time')

Error

The error I am getting is "??? Undefined function or method 'integral' for input arguments of type 'double'.

Error in ==> MIT at 29 vs = integral(fun,0,vg);""


Solution

  • Reading your title again it sounds like you want to integrate the constant from 0 to different bounds. So there were two things wrong:

    1. You're function wasn't a function just a double, to fix this change make fun an anonymous function like this

      fun=@(t) (1-(c_t/c_g))*ones(size(t));
      

      The ones(size(t)) just makes it so that there is an output for each input.

    2. You need to have a single upper and lower bound for integral, to fix this stick your integral in an arrayfun, like this:

      vs = arrayfun(@(xu) integral(fun,0,xu), vg);
      

      For more information on arrayfun.

    With the entire code here

    epsilon0=8.85*10^-12;
    d_mos=6*10^-9;
    epsilon_mos=5*epsilon0;
    d_g=30*10^-9;
    epsilon_g=19*epsilon0;
    vt=-2;
    e=1.6*10^-19;
    n=[];
    i=1;
    t2=[];
    u=60; % cm^2/v*S
    h=1.05*10^-34;  % ev*s
    
    
    %Capacitor Calaculation
    c_g=(epsilon_g/d_g);
    c_mos=(epsilon_mos/d_mos);
    c_t=1/((1/c_g)+(1/c_mos));
    
    
    %Input Voltage 
    t = 0:0.2:10;
    vg = 5*sin(t);
    
    %Surface Voltage
    fun=@(t) (1-(c_t/c_g))*ones(size(t));
    vs = arrayfun(@(xu) integral(fun,0,xu), vg);
    
    figure 
    plot(t,vs)
    title('vs vs time')