Integrating Function with MATLAB - Solve & Plot

In summary, the conversation discusses a problem with finding an integral of a function and plotting it with its derivative. The code provided has an error due to trying to store a vector in a scalar's place. The solution is to change t to t(n) and properly define t. The question then asks if it is possible to integrate Xdot to X in M-code.
  • #1
Execute
2
0
Hi everyone, I have a problem in finding an integral of a function. I'm given the following function: x(t) = t + 1 (-1 <= t < o ); e^-t (0<= t <= 2); 0 (otherwise).I have to find the integral and plot it with its derivative and the given function.

Here is my code:
%Set up integral vector
u(1:1000) = 0;
%Find the integral
for n = 1:999;
if( (t(n)>= -1) & (t(n)<0) );
u(n) = 0.5.*(t.^2) + t;
end
if ( (t(n)>=0) & (t(n)<2) );
u(n) = -1.*(exp(-t));
end
if( (t(n)>2 ) )
u(n) = 1.*t;
end
end
subplot(3,1,3);
plot(t,u);

I get the following error when trying to run the code:
? In an assignment A(I) = B, the number of elements in B and
I must be the same.

Error in ==> tut1 at 32
u(n) = 0.5.*(t.^2) + t;
 
Physics news on Phys.org
  • #2
Execute said:
Error in ==> tut1 at 32
u(n) = 0.5.*(t.^2) + t;

In these three lines
Code:
     u(n) = 0.5.*(t.^2) + t;
      u(n) = -1.*(exp(-t));
      u(n) = 1.*t;
you are trying to store a vector in a scalar's place. u(n) is a scalar and t is a vector, so change t to t(n) and you will be fine. Also, be sure to define t properly (don't see it in your code), e.g. t = linspace(-1,2,1000).
 
  • #3
i have 1 question
how do i apply 1/s in simulink block to M-code,
for example
Xdot to X
in simulink, we just apply the 1/s(integration block).
so i just want to know, is it possible to integrate the Xdot to X in m code??
 

1. What is MATLAB and how is it used in integrating functions?

MATLAB is a programming language and interactive computing environment commonly used by scientists and engineers for data analysis, visualization, and mathematical computations. In integrating functions, MATLAB is used to solve and plot mathematical expressions or functions.

2. How do I define and manipulate functions in MATLAB?

To define a function in MATLAB, you can use the "function" keyword followed by the function name and its input arguments. Then, write the code for the function inside the function block. To manipulate functions, you can use various built-in functions and operators in MATLAB such as "sin", "cos", "*", "/", etc.

3. Can I integrate multiple functions in MATLAB?

Yes, you can integrate multiple functions in MATLAB by using the "syms" command to define symbolic variables and then using the "int" command to integrate the functions with respect to these variables.

4. How do I plot the integrated function in MATLAB?

To plot the integrated function in MATLAB, you can use the "ezplot" or "fplot" commands. These commands allow you to specify the function, the range of values for the independent variable, and the plot style.

5. Can I save and export the plotted function in MATLAB?

Yes, you can save and export the plotted function in MATLAB by using the "saveas" or "exportgraphics" commands. These commands allow you to save the plot in various file formats such as PNG, PDF, or EPS.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
238
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
202
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
978
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
925
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
829
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
Back
Top