How to evaluate different expressions of a matrix for multiple variable values?

2 Ansichten (letzte 30 Tage)
Hi guys, I have a little problem using the eval/feval function... I have a vector/matrix whose rows contain symbolic functions, which are dependent on time. I want to evaluate these values for different time values. The code is below:
r1 =
e*cos(omega*t)
e*sin(omega*t)
0
e = 50;
omega = 20;
t=0:0.001:10;
r1_final = feval(r1,t)
So I gave "e" and "omega" arbitrary values (50 and 20) and also t=0:0.001:10. Using the command bellow I get this error:
"Error using feval Argument must contain a string or function_handle."
How can I transform each row into a function_handle and t to a string?? Thank you!
  2 Kommentare
Stephen23
Stephen23 am 20 Mär. 2015
Avoid using feval and eval for such trivial code as this. Learn about anonymous functions and using function handles instead: your work will be much more reliable, be easier to debug and it lets you use the inbuilt code-hinting and code-checking tools. eval and feval are best avoided, and certainly should not be your "standard tool" for basic code like this.
Lucas Carvalho
Lucas Carvalho am 20 Mär. 2015
Thank you for the advice Steven... I think it's awesome people like you giving easier ways to do things on Matlab! I've just started writing big scripts and for sure the debug time is a important factor... once more, thank you!

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Andrei Bobrov
Andrei Bobrov am 20 Mär. 2015
r1 = @(t,e,omega)[e*[cos(omega*t);e*sin(omega*t)];zeros(1,numel(t))];
e = 50;
omega = 20;
t=0:0.001:10;
out = r1(t, e, omega);

Weitere Antworten (0)

Kategorien

Mehr zu Characters and Strings finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by