Matlab function to calculate

376 Views Asked by At

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?

2

There are 2 best solutions below

12
On

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 
0
On

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.