2

How can I input the two functions from this task in matlab ?
A voltage peak in a circuit is caused by a current through a resistor.
enter image description here

The energy E which is dissipated by the resistor is:
enter image description here
Calculate E if
enter image description here

This is my final solution:

function E = calculateE(i0,R,t0)
syms t;
f = i0*exp(-t/t0) * sin(2*t/t0);
E = int(R*f*f,0,inf);
end

Question: how can I input the above functions in matlab to calculate the result?

  • What do you want to calculate? $E$? – Ribz May 16 '14 at 16:25
  • yes, but I probably don't want to use the final formula.. but do it all in matlab – Iulian Rosca May 16 '14 at 16:27
  • MATLAB is not well-suited for symbolic computation unless you use the symbolic toolbox, but even then this is not a very good problem for that. It is still completely unclear whether you want MATLAB to be computing the integral or what. What are your inputs/outputs? – Emily May 16 '14 at 16:28
  • the inputs are i0, R, and t0.. and I want to calculate E , integral of the function – Iulian Rosca May 16 '14 at 16:30
  • So if $I_0=100$, $t_0=0.01$, $R=0.5$, you want to compute $100^20.010.5/5$? What is the problem computing that exactly? – hejseb May 16 '14 at 16:33
  • actually the solution is irelevant , how can I input the functions i(t) and the integral in matlab? – Iulian Rosca May 16 '14 at 17:01
  • You either need to use the symbolic toolbox or choose a numerical quadrature routine. There is no built-in function for generalized integration in MATLAB. – Emily May 16 '14 at 17:10
  • can you show an example? – Iulian Rosca May 16 '14 at 17:12

2 Answers2

0

Forgive me if I am misunderstanding your question. Put that function code in a file named calculateE.m in the directory that you are running MATLAB in. On the MATLAB command line, or in another MATLAB function file, the line myE = calculateE( myi0, myR, myt0); will call the function using values myi0, myR, myt0 and return the result myE.

Mark
  • 79
0

You can write a MATLAB function like

function E = calculateE(i0,R,t0)
  % The function i(t) 
  function y = i(t)
    y = i0 .* exp(-t/t0) .* sin(2.*t/t0); 
  end  

  function y = intFunction(t)
    y = R * i(t).^2;
  end

  % Calculate energy 
  E = integral(@intFunction,0,Inf);
end 
Carser
  • 3,400