-1

I am new to GNU Octave and I want to plot the function psi in the range of [0 : 2000]:

function y = H(x)
  if (x > 0)
    y = 1
  else
    y = 0
  endif
endfunction

function y = psi(s)
  t = 200
  phiabs = 500
  K = 1000
  n0 = 1000
  y = -n0 * e .^ (-(s - phiabs) / t) * H(s - phiabs) - K * H(s) * H(phiabs - s)
endfunction

How to do that?

Adriaan
  • 17,741
  • 7
  • 42
  • 75
Erhard Dinhobl
  • 1,646
  • 18
  • 34

1 Answers1

3

First of all, you'll want to modify your psi and H functions to be able to perform element-wise operations so that you can pass an array of s values to it and receive an array of y values

function y = H(x)
    y = double(x > 0);
endfunction

function y = psi(s)
  t = 200
  phiabs = 500
  K = 1000
  n0 = 1000
  y = -n0 * exp(-(s - phiabs) ./ t) .* H(s - phiabs) - K .* H(s) .* H(phiabs - s)
endfunction

Also note that instead of e.^() you'll need to use exp.

Then you'll want to use plot to plot the result in a figure

s = 0:2000;
y = psi(s);

plot(s, y)
Suever
  • 64,497
  • 14
  • 82
  • 101