02 scilab and vissim.pdf

7
SciLab, MATLAB, and MultiSim SciLab & MATLAB SciLab and MATLAB are very similar programs. They both turn a PC into a calculator which can manipulate matricies, handle complex numbers, and plot the results. MATLAB is the official language of ECE. SciLab is very similar to MATLAB and is free (!). We'll be using SciLab throughout this semester. If you use Matlab, the syntax presented in these lecture notes should also work. VisSim is similar to Matlab's Simulink. It is a graphical tool that lets you solve differential equations, such as finding the voltages in a circuit with capacitors. More on this shortly. Some of the things we'll be doing with SciLab this semester is Plotting functions Solving 2 equations for 2 unknowns, and Doing calculus. Plotting Functions in SciLab & MATLAB: The syntax in SciLab is as follows: [ start of a matrix ] end of a matrix , next column ; next row within a matrix, don't display the results at the end of a line ' transpose plot(x,y) plot x vs. y For example, plot the function y = 3 + 4 sin (10t)+ 2 cos (50t) In SciLab: t = [0:0.01:3]'; y = 3+4*sin(10*t)+2*cos(50*t); plot(t,y); xlabel('seconds'); ylabel('y'); xgrid(5) Next, find the derivative and integral of y(t). The derivative is . Numerically, dy dt dy dt y(i)−y(i1) t(i)t(i1) NDSU 3: SciLab ECE 321 - JSG 1 August 24, 2012

Upload: toannguyenkhanh

Post on 04-Oct-2015

5 views

Category:

Documents


0 download

TRANSCRIPT

  • SciLab, MATLAB, and MultiSimSciLab & MATLABSciLab and MATLAB are very similar programs. They both turn a PC into a calculator which can manipulatematricies, handle complex numbers, and plot the results. MATLAB is the official language of ECE. SciLab isvery similar to MATLAB and is free (!). We'll be using SciLab throughout this semester. If you use Matlab, thesyntax presented in these lecture notes should also work.

    VisSim is similar to Matlab's Simulink. It is a graphical tool that lets you solve differential equations, such asfinding the voltages in a circuit with capacitors. More on this shortly.

    Some of the things we'll be doing with SciLab this semester isPlotting functionsSolving 2 equations for 2 unknowns, andDoing calculus.

    Plotting Functions in SciLab & MATLAB:The syntax in SciLab is as follows:

    [ start of a matrix] end of a matrix, next column; next row within a matrix, don't display the results at the end of a line' transposeplot(x,y) plot x vs. y

    For example, plot the function

    y = 3 + 4 sin (10t) + 2 cos (50t)

    In SciLab:

    t = [0:0.01:3]';y = 3+4*sin(10*t)+2*cos(50*t);plot(t,y);xlabel('seconds');ylabel('y');xgrid(5)

    Next, find the derivative and integral of y(t). The derivative is . Numerically,dydtdydt

    y(i)y(i1)t(i)t(i1)

    NDSU 3: SciLab ECE 321 - JSG

    1 August 24, 2012

  • or

    -->size(y) 301. 1. -->dy = 0*y; -->for i=2:300--> dy(i) = (y(i+1) - y(i-1)) / 0.02;--> end -->plot(t,dy)

    Note that differentiation amplifies the high-frequency terms. The 50 rad/sec term dominates the result.

    The integral is

    0t y dt

    i=0

    ty(i) dt

    in SciLab

    -->iY = 0*y; -->for i=2:301--> iY(i) = iY(i-1) + y(i)*0.01;--> end -->plot(t,iY)

    Note that integration amplifies the low-frequency terms. The resulting signal is dominated by a ramp: theintegral of a constant is a ramp.

    NDSU 3: SciLab ECE 321 - JSG

    2 August 24, 2012

  • You can also use SciLab to solve for 2 equations and 2 unknowns. For example, when we get to diodes, we'll tryto solve the following two equations for V and I:

    V = 5 I

    V = 0.0526 ln (105 I + 1)

    In SciLab-->I = [0:0.01:5]';-->V1 = 5-I; -->V2 = 0.0526*log(1e5*I+1); -->plot(V1,I,V2,I);-->xlabel('Volts');-->xgrid(5)-->ylabel('mA');

    The solution from the graph is where the two graphs interesect. You can also find this numerically. Guess I.Find V from equation 1 and 2Find the difference in V. This gives a function of I whose solution is when f(I)=0.Use a solver (such as Newton's method) to find I:

    The results is (0.6825V, 4.3175mA)

    Frequency Response in SciLab:SciLab also works for complex numbers. For example, find the amplitude of G(s) from 0 to 10 Hz:

    G(s) = 5ss2+5s+100

    -->f = [0:0.01:10]';-->w = 2*%pi*f;-->j = sqrt(-1);-->s = j*w;-->G = 5*s ./ (s.^2 + 5*s + 100); -->plot(f,abs(G))-->xlabel('Hz');-->ylabel('gain');

    NDSU 3: SciLab ECE 321 - JSG

    3 August 24, 2012

  • Adding a function in MATLAB:Create a file and give it an extension of .m. (for example, call the funciton jake.m ). When you type in the wordjake

    Matlab uses the variable with the name of jake. If this doesn't exist,Matlab looks for the file with the name jake.m in the current directory and executes it. If this doesn't exist,Matlab looks in the default directory. If still no file is found,Matlab gives you an error.

    Adding a function in SciLab:Adding a funciton in Scilab is a little harder. Suppose you want to add a function 'dB' which is called as

    y = dB(x)

    In SciLab, go to File Change Current Directory. Change it to where your files are located.

    Go to File Open a file. Open up a .sci file. Change it as follows:

    NDSU 3: SciLab ECE 321 - JSG

    4 August 24, 2012

  • note: SciLab is very picky.The file name must patch the function nameBoth are case sensitive

    Once you have a file you're happy with, add this to SciLab (execute - load into Scilab)

    note: If you create a bunch of files, you can load them into Scilab one at a time. Save the environment. Whenyou restart Scilab, load the environment and all the functions that were in Scilab before are there again.

    NDSU 3: SciLab ECE 321 - JSG

    5 August 24, 2012

  • MultiSimMultiSim is available on the cluster computers. It allows you to run nonlinear numerical simulations of circuits.Typically, each homework set should include:

    Analysis: Using simplifying approximations (such as the voltage across a diode is 0.7V and there is noresistance), calculate values for resistors and compute what the currents and voltages should be.Simulation: Using MultiSim, check your answers with a more complex (and more accurate, but stillimpefect) toolHardware: Determine the actual currents, voltages etc. with real components.

    All results should be similar - but slightly different due to the simplifying approximations used.

    For example, determine the voltage at y(t) for x(t) being a 5Vp 1kHz sine wave with Multi-Sim.

    +

    -x(t)

    R1

    CR2

    I1 I2I0 +

    -

    y(t)

    In MultiSim, Add the components using the drop-down menu bars on the topConnect components using click and dragAdd a multimeter or oscilloscope from the menu on the rightClick the on-off switch

    For print-outs, reverse the image of the o-scope

    The net result will look like the following:

    NDSU 3: SciLab ECE 321 - JSG

    6 August 24, 2012

  • What this tells you is:The DC value (average) output voltage is 4.152 voltsThe output has some ripple - as shown on the oscilloscopeIf you switch the volt meter to AC, the ripple is 0.113V rms.

    Presumably, you could calculate these numbers as well as measure these with hardware for comparison.

    NDSU 3: SciLab ECE 321 - JSG

    7 August 24, 2012