# MATLAB Code help!

• MATLAB
Hello,

First of all, I am not trying to "spam" subforums. I found out that my thread shouldn't be posted under homework. Anyways, here it is.

Integration
Let say there's a polynomial, 5x+6 and you want to integrate from 0 to 3 respect to x, how do you input in MATLAB? (I guess you can't figure out the constant once you integrate it since you don't have initial conditions. Ignore that constant part unless it's needed to execute the command.)

My major problem is that the MATLAB doesn't want to execute anything unless I specify x prior to that, which means it doesn't want to work in variable forms.

Bottomline, I want that equation integrated from 0 to 3 and substitute 100 into x to obtain value.

Derivative
Same as above, if I want to take a derivative of (x^2)+6 where x=2, what do I need to input in MATLAB?

I've tried integrate, int, diff, and many other commands that I found under MATLAB but it doesn't do what I want it to do. (diff basically takes the difference but one source told me that it's for derivative.)

Thanks!

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
If you don't actually need to produce the code, you can just plug it into www.wolframalpha.com. Also, to solve variables in MatLab, I believe you have to have certain packages installed. But:

Type: syms x -this sets x as a symbolic variable
Hit Return
Type: int(5x+6,x,0,3)
Hit return

The problem you are probably having is you need to make sure you declare you variable as symbolic

If you don't actually need to produce the code, you can just plug it into www.wolframalpha.com. Also, to solve variables in MatLab, I believe you have to have certain packages installed. But:

Type: syms x -this sets x as a symbolic variable
Hit Return
Type: int(5x+6,x,0,3)
Hit return

The problem you are probably having is you need to make sure you declare you variable as symbolic
syms doesn't work...here's what message i get:

??? Undefined function or method 'syms' for input arguments of type 'char'.

cronxeh
Gold Member
Which version of Matlab do you have? You need symbolic math module. Maybe you should try

x = sym('x')

Which version of Matlab do you have? You need symbolic math module. Maybe you should try

x = sym('x')
i still get the same error and i am using 7.8.0 (R2009a). thanks!

cronxeh
Gold Member
i still get the same error and i am using 7.8.0 (R2009a). thanks!
Type ver and tell me what the output is

I'm getting (among many other things):

Symbolic Math Toolbox Version 5.1 (R2008b)

Type ver and tell me what the output is

I'm getting (among many other things):

Symbolic Math Toolbox Version 5.1 (R2008b)
>> ver
-------------------------------------------------------------------------------------
MATLAB Version 7.8.0.347 (R2009a)
Operating System: Microsoft Windows Vista Version 6.0 (Build 6002: Service Pack 2)
Java VM Version: Java 1.6.0_04-b12 with Sun Microsystems Inc. Java HotSpot(TM) 64-Bit Server VM mixed mode
-------------------------------------------------------------------------------------
MATLAB Version 7.8 (R2009a)
Bioinformatics Toolbox Version 3.3 (R2009a)
Control System Toolbox Version 8.3 (R2009a)
Curve Fitting Toolbox Version 2.0 (R2009a)
Image Processing Toolbox Version 6.3 (R2009a)
Instrument Control Toolbox Version 2.8 (R2009a)
Optimization Toolbox Version 4.2 (R2009a)
Signal Processing Blockset Version 6.9 (R2009a)
Signal Processing Toolbox Version 6.11 (R2009a)
SimMechanics Version 3.1 (R2009a)
Simscape Version 3.1 (R2009a)
Simulink Control Design Version 2.5 (R2009a)
Stateflow Version 7.3 (R2009a)
Statistics Toolbox Version 7.1 (R2009a)

seems like i am missing that. it's not available from my school website. i am guessing this isn't free...?

But, I think I should be able to solve this numerically if I input numbers to each variables, no?

I've been manually integrate by hand and input it in MATLAB to find values, which totally defeats the purpose of using computer. :(

cronxeh
Gold Member
Well mine was free, but I am not allowed to talk about it f95toli
Gold Member
Matlab isn't really a good package for symbolic math....

Anway, to integrate numerically you can e.g. use the trapz function

Try searching for "trapz" in the help-files (which, btw, are very good so you should learn to use them), note that the output of "help trapz" is very cryptic, but it you look in the help
(the one that appears when you press F1 in Windows) you will find an example where they integrate sin(X)

Generally speaking you need to define the intervall you want to integrate over using for example

Code:
x=linspace(0,3,100);
This will create an array with 100 elements ranging from 0 to 3 (the ";" is to prevent Matlab from printing the result in the command window)

Then you define the function

Code:
y=5*x+6;
if you want you can now have a look at what this function looks like using

Code:
plot(x,y)

to integrate using trapz you write

Code:
trapz(x,y)
"diff" works in more or less the same way, there is an example in the help.

Matlab isn't really a good package for symbolic math....

Anway, to integrate numerically you can e.g. use the trapz function

Try searching for "trapz" in the help-files (which, btw, are very good so you should learn to use them), note that the output of "help trapz" is very cryptic, but it you look in the help
(the one that appears when you press F1 in Windows) you will find an example where they integrate sin(X)

Generally speaking you need to define the intervall you want to integrate over using for example

Code:
x=linspace(0,3,100);
This will create an array with 100 elements ranging from 0 to 3 (the ";" is to prevent Matlab from printing the result in the command window)

Then you define the function

Code:
y=5*x+6;
if you want you can now have a look at what this function looks like using

Code:
plot(x,y)

to integrate using trapz you write

Code:
trapz(x,y)
"diff" works in more or less the same way, there is an example in the help.
Thank you. I've looked in help, been to mathworks, etc.

I am guessing the derivative should be done as follows for y=5x+6:

a=[5 6];
z=polyder(a)

And I do understand it output coefficients in order from the lowest power to the highest. Thank you!

When you integrate polynomials, you get the constant, which can be identified based on initial condition. If you know the value for this constant, how do you specify it in MATLAB? Thanks

hi , i think i can help you with your "syms" problem , i looked for it in mathworks and they said that the symbolic math tollbox isnt supported by 64-bit systems (for some matlab versions not all) , and they said that you can install the 32-bit version on a 64-bit system manually from the same matlab DVD , i havent tried it but i think its not that hard ....
here is the mathwroks link to it :

http://www.mathworks.com/support/solutions/en/data/1-579TVF/index.html?solution=1-579TVF

tell me if this works for you :)

Hi;
I have got a matlab program like this

h=4.14*10^-15;%eV.s
c=3*10^8%m/s^2
d=211.7*10^-9%m;
lambda= (WNBTIO21)*10^-9%m
T=(TNBTIO21)./100;
R=(RNBTIO21)./100;
y=(log((1-R)./T));
alpha=y*(1/d);
nu=(1./lambda).*c;
A=nu.*(alpha).*(h);
D=sqrt(A)
B=nu.*(h);
plot (B,D, 'or')
grid minor;

and i have another data
T=TNBTIO19
R=RRNBTIO19
how can I include these data in the program above to have both plot in a single axes?

jhae2.718
Gold Member
If you want both sets of data to be plotted on the same figure, use:
Code:
 hold on;