logo elektroda
logo elektroda
X
logo elektroda
REKLAMA
REKLAMA
Adblock/uBlockOrigin/AdGuard mogą powodować znikanie niektórych postów z powodu nowej reguły.

[Matlab] Co zrobić by nie obliczał funkcji od razu?

~sojer 19 Lis 2006 14:44 2875 3
REKLAMA
  • #1 3240422
    ~sojer
    Poziom 27  
    Posty: 921
    Pomógł: 91
    Ocena: 24
    Witam.
    Piszę właśnie niewielki skrypcik, który ma dla dowolnej zadanej funkcji zrobić kilka obliczeń. Problem tkwi w tym, że przykładową funkcję "f=3x+2sin(x)" muszę obliczyć kilkadziesiąt razy zmieniając "x", a Matlab od razu po zadaniu funkcji oblicza ją. Co zrobić by móc wczytać funkcję w oryginalnej postaci, a dopiero potam ją obliczać?
    Próbowałem już wczytać ją jako string, ale nie wiem jak ją później wykorzystać.
  • REKLAMA
  • Pomocny post
    #2 3240515
    maciek_slon
    Poziom 29  
    Posty: 942
    Pomógł: 143
    Ocena: 27
    Jak masz stringa to możesz użyć funkcji eval do obliczenia wartości. W helpie wklep eval, będziesz miał tam wiele innych przykładów, łącznie z funkcją która ze stringa robi zmienną typu funkcyjnego :-)

    Niestety matlaba nie mam na dysku w tej chwili i nie za bardzo mogę pomóc Ci dokładniej.
  • REKLAMA
  • Pomocny post
    #3 3240528
    kmg
    Poziom 13  
    Posty: 26
    Pomógł: 4
    Ocena: 1
    A nie lepiej jako x podawać wektor z wartościami dla ktorych chcesz obliczyc ową funkcje?
    Najpierw zgromadzić wartości w wektorze, potem obliczać.
    Jako wynik dostaniesz równiez wektor, na ktorym latwo dalej operować.

    Eval to armata, nie wiem czy jej potrzebujesz
    
    >> help eval
     EVAL Execute string with MATLAB expression.
        EVAL(s), where s is a string, causes MATLAB to execute
        the string as an expression or statement.
     
        EVAL(s1,s2) provides the ability to catch errors.  It
        executes string s1 and returns if the operation was
        successful. If the operation generates an error,
        string s2 is evaluated before returning. Think of this
        as EVAL('try','catch').  The error string produced by the
        failed 'try' can be obtained with LASTERR.
        
        [X,Y,Z,...] = EVAL(s) returns output arguments from the
        expression in string s.
     
        The input strings to EVAL are often created by 
        concatenating substrings and variables inside square
        brackets. For example:
     
        Generate a sequence of matrices named M1 through M12:
     
            for n = 1:12
               eval(['M' num2str(n) ' = magic(n)'])
            end
     
        Run a selected M-file script.  The strings making up 
        the rows of matrix D must all have the same length.
        
            D = ['odedemo '
                 'quaddemo'
                 'fitdemo '];
            n = input('Select a demo number: ');
            eval(D(n,:))
     
        See also feval, evalin, assignin, evalc, lasterr.
    
    
  • #4 3240578
    ~sojer
    Poziom 27  
    Posty: 921
    Pomógł: 91
    Ocena: 24
    Dzięki za szybką odpowiedź. Dokładnie o to mi chodziło.
REKLAMA