Matlab Difference Equation Loop?

However, Matlab gives an error message saying that the nth term needs to be a scalar number, not a variable. The problem is how to change the nth term in order to get the desired result.
  • #1
Dukefool
5
0
Trying to make a three line loop that would differentiate x2√4x+1 to the nth term starting from the original function (n=0) to the 5th derivative (n=5) and then substitute 2 into the derivatives. Here's what I got

f = @(x) x2√4x+1
For n=[0:5] - As the nth term goes from 0 to 5
disp(num2str(subs(diff(f(x),n),2)))
end

Matlab says that the nth term must be an actual scalar number instead of a variable. I want the nth term to change so how can I do that?
 
Physics news on Phys.org
  • #2
Dukefool said:
Trying to make a three line loop that would differentiate x2√4x+1 to the nth term starting from the original function (n=0) to the 5th derivative (n=5) and then substitute 2 into the derivatives. Here's what I got

f = @(x) x2√4x+1
For n=[0:5] - As the nth term goes from 0 to 5
disp(num2str(subs(diff(f(x),n),2)))
end

Matlab says that the nth term must be an actual scalar number instead of a variable. I want the nth term to change so how can I do that?

for n = 0:5
 

Related to Matlab Difference Equation Loop?

What is a difference equation in Matlab?

A difference equation in Matlab is an equation that describes the relationship between the current value of a variable and its previous values. It is used in time series analysis and is commonly used in signal processing, control systems, and other scientific and engineering applications.

What is the syntax for creating a difference equation loop in Matlab?

The syntax for creating a difference equation loop in Matlab is as follows:

for i = 2:n

y(i) = a*y(i-1) + b*x(i) + c;

end

Where n is the number of iterations, a, b, and c are coefficients, y is the output variable, and x is the input variable.

How do I specify initial conditions for a difference equation loop in Matlab?

To specify initial conditions for a difference equation loop in Matlab, the initial value of the output variable must be defined before the loop begins. This can be done by setting the first element of the output variable to the desired initial value, for example: y(1) = 0;.

Can I use vector operations in a difference equation loop in Matlab?

Yes, vector operations can be used in a difference equation loop in Matlab. This can improve the efficiency and speed of the loop by avoiding the use of for loops and relying on the built-in vector operations of Matlab.

What are some common errors when creating a difference equation loop in Matlab?

Some common errors when creating a difference equation loop in Matlab include forgetting to specify initial conditions, using incorrect syntax, and not defining all necessary variables. It is also important to check for any division by zero errors and ensure that all variables are of the same size.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
10
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
173
  • MATLAB, Maple, Mathematica, LaTeX
Replies
14
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top