Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Another Matlab :

  1. May 6, 2009 #1
    The problem is: Compare the performance of the forward, backward, and central difference methods for estimating the derivative of the following function: y(x)=e^-xsin(3x). Use 101 points from x = 0 to x = 4. Use a random additive error of +/- 0.01.

    I understand the process of solving this one but I keep getting errors with how I enter my function, I still dont get all of the rules for those. What I am using is
    y = exp(-x)*sin(3x) Im guessing theres something wrong with this? Ill enter the rest of my entry to make it easier:

    >> x = linspace(0,4,101);
    n = length(x);
    % The sinusoidal funciton
    y = exp(-x)*sin(3.x);
    % added with measurment errors;
    ym = y + 0.01*2*(rand(1,n)-0.5);
    % Note: rand(1,n) returns a vector of uniformly distributed random numbers
    % between 0.0 and 1.0
    figure(1);
    plot(x,y,'k-', x,ym,'bo');
    xlabel('x');
    ylabel('y');
    legend('y=exp(-x)*sin(3x)','meared signal with noise');
    % The true derivative
    dyt = -exp(-x)*sin(3x) + 3exp(-x)*cos(3x);
    % backward difference
    dyb = diff(ym)./diff(x);
    xb = x(2:n);
    % forward difference
    dyf = diff(ym)./diff(x);
    xf = x(1:n-1);
    % central difference
    dyc = gradient(ym)./gradient(x);
    xc = x;
    figure(2);
    plot(x,dyt,'k');
    hold on;
    plot(xf, dyf, 'bo');
    plot(xb, dyb, 'gs');
    plot(xc, dyc, 'r^');
    legend('true derivative','forward difference', 'backward','central');
    xlabel('x');
    ylabel('dy/dx');
    ??? y = exp(-x)*sin(3x);
    |
    Error: Unexpected MATLAB expression.
     
  2. jcsd
  3. May 7, 2009 #2

    Hootenanny

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    Try y = exp(-x)*sin(3*.x)

    In MATLAB you have to explicitly write out each mutliplication (and division), otherwise MATLAB treats '3x' as a variable, rather than '3 times x'.
     
    Last edited: May 7, 2009
  4. May 7, 2009 #3
    In Matlab you define an inline function like this

    >> y = inline('exp(-x).*sin(3*x)')

    the .* instead of simply * is because x may be a vector, so we want an element by element product, which returns a vector. Note that the inverted commas are necessary.

    EDIT: silly me, you don't need an inline function, you need this:

    >> exp(-x).*sin(3*x);
     
    Last edited: May 7, 2009
  5. May 7, 2009 #4
    Thanks for the help guys I appreciate it.

    Now I have another problem though, ha, for the true derivative I'm getting another error now. Dont know if I entered the wrong derivative or what I did?

    This is what I got:

    % The true derivative
    dyt = -exp(-x)*sin(3x) + 3exp(-x)*cos(3*x);

    and the error:
    ??? dyt = -exp(-x)*sin(3x) + 3exp(-x)*cos(3*x);
    |
    Error: Unexpected MATLAB expression.

    Thanks Again
     
  6. May 8, 2009 #5
    x is a vector right? So exp(-x) returns a vector, and sin(3*x) returns a vector too. The multiplication you are trying to do is element by element, so you require .* and NOT *, e.g.
    >> [2 5 7].*[3 3 2] = [6 15 14]
    However
    >> [2 5 7]*[3 3 2]
    is incorrect; it is not a well-formed expression in Matlab. * is used when one of the things you are multiplying together is a scalar, so it is correct to use * in the following situations:
    >> 5*[1 5] = [5 25]
    >> 5*3 = 15
    >> [0 4]*3 = [0 12]
    If you want to multiply two vectors element-by-element, you have to do it as I showed above, using .*
    Remember WHENEVER you multiply two things, it doesn't matter what they are, you need to use * or .*, you don't simply put them next to each other. So this is wrong:
    >> 3[1 2]
    Similar comments apply to the other operation ^,\, but not to addittio and subtraction, but remember to only add and subtract like things, e.g. vectors of the same length, matrices of the same size (both 5x3 or whatever) etc. If you are unsure of whether to us * or .*, or + or .+, just remember that a dot before an operation ALWAYS works, no matter what the objects used (provided all else is correct), so the following all work:
    >> 3*[3 1 6] = [9 3 18]
    >> 3.*[3 1 6] = [9 3 18]
    >> [4 5 2] + [1 1 6] = [5 6 8]
    >> [4 5 2] .+ [1 1 6] = [5 6 8]
    and so on.
    Remember, computers are VERY finnicky about what you type. Garbage in, garbage out. They hav been programmed to accept exact statements, and if you don't enter your commands exactly as the program was written, the program cannot guess what you are trying to.
    The correct expression you were looking for is:
    >> dyt = -exp(-x).*sin(3*x) + 3*exp(-x).*cos(3*x);
    Do you understand why?
    Cheers.
     
  7. May 9, 2009 #6

    You should have typed 3*exp(-x)*cos(3*x)
    The * was missing. :biggrin:
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Another Matlab :
  1. Another Matlab problem (Replies: 4)

  2. Another Matlab Problem (Replies: 2)

Loading...