MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

### Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

# Thread Subject: problem with my code

Subject: problem with my code

From: Theo Kalfas

### Theo

(Theo Kalfas)

Date: 25 Jan, 2009 10:56:09

Message: 1 of 11

hi!
I have a problem with my program and i need your help.
I have this code:

v=-0.1;
A=12.56e-4;
L=0.44;
G=2;
T=300;
q=1.6e-19;
k=8.62e-5;
Edot=0.05;
Ndot=5e10;
DE=0.3;
syms E;
denom=1+exp((E-(v/L))/(k*T));
function1=1./denom;
static=(G*Ndot)/(sqrt((pi*DE)/2));
ek8eths=((E+Edot+(v/L))/DE).^2;
function2=exp(-2*ek8eths);
function3=function1.*static;
final=function3.*function2;

%or if i try final=function1.*function2.*static is correct?

subtotal=double(int(final,E,0,inf))

When i give values for v between -12:0.05:-0.5 and 0.5:0.05:2 the the program gives me the results which i want.if i try to give values between -0.5:0.05:0 and 0:0.05:0.5 then the matlab gives me this error:
??? Error: Assignment statements do not produce results. (Use == to test
for equality.)

Error in ==> C:\MATLAB6p5\toolbox\symbolic\@sym\double.m
On line 45 ==> D = reshape(eval(X),m,n);

Error in ==> C:\MATLAB6p5\work\otinanai.m
On line 25 ==> subtotal=double(int(final,E,0,inf))

The worst is that when i give the value v=0 then the program is busy and never stops!!What can i do?
thank you!!

Subject: problem with my code

From: Joerg Buchholz

### Joerg Buchholz

Date: 25 Jan, 2009 16:47:06

Message: 2 of 11

"Theo Kalfas" <k_thodoros@yahoo.gr> wrote in message <glhgg9$ac6$1@fred.mathworks.com>...
> hi!
> I have a problem with my program and i need your help.
> I have this code:
>
> v=-0.1;
> A=12.56e-4;
> L=0.44;
> G=2;
> T=300;
> q=1.6e-19;
> k=8.62e-5;
> Edot=0.05;
> Ndot=5e10;
> DE=0.3;
> syms E;
> denom=1+exp((E-(v/L))/(k*T));
> function1=1./denom;
> static=(G*Ndot)/(sqrt((pi*DE)/2));
> ek8eths=((E+Edot+(v/L))/DE).^2;
> function2=exp(-2*ek8eths);
> function3=function1.*static;
> final=function3.*function2;
>
> %or if i try final=function1.*function2.*static is correct?
>
> subtotal=double(int(final,E,0,inf))
>
> When i give values for v between -12:0.05:-0.5 and 0.5:0.05:2 the the program gives me the results which i want.if i try to give values between -0.5:0.05:0 and 0:0.05:0.5 then the matlab gives me this error:
> ??? Error: Assignment statements do not produce results. (Use == to test
> for equality.)
>
> Error in ==> C:\MATLAB6p5\toolbox\symbolic\@sym\double.m
> On line 45 ==> D = reshape(eval(X),m,n);
>
> Error in ==> C:\MATLAB6p5\work\otinanai.m
> On line 25 ==> subtotal=double(int(final,E,0,inf))
>
> The worst is that when i give the value v=0 then the program is busy and never stops!!What can i do?
> thank you!!

Theo,
you can use 'matlabFunction' to generate an anonymous function from your symbolic function and the compute the integral numerically:

anonymous_final = matlabFunction (final)
subtotal = quad (anonymous_final, 0, 1e12)

If your Matlab is to old to know 'matlabFunction', you can generate an inline function:

inline_final = vectorize (inline (char (final)))
subtotal = quad (inline_final, 0, 1e12)

Subject: problem with my code

From: Theo Kalfas

### Theo

(Theo Kalfas)

Date: 26 Jan, 2009 11:54:18

Message: 3 of 11

> Theo,
> you can use 'matlabFunction' to generate an anonymous function from your symbolic function and the compute the integral numerically:
>
> anonymous_final = matlabFunction (final)
> subtotal = quad (anonymous_final, 0, 1e12)
>
> If your Matlab is to old to know 'matlabFunction', you can generate an inline function:
>
> inline_final = vectorize (inline (char (final)))
> subtotal = quad (inline_final, 0, 1e12)

Thank you very much Joerg!
Now,i have one other question.I want this code to find the subtotal for v=-12:0.05:2 and after i want the derivative for all of these like this method (c2-c1)/(v2-v1),(c3-c2)/(v3-v2) etc.The way that i'am thinking is:
To create two M-files with names integral and derivative.
Now the M-file integral is:

function subtotal=integral(v)
A=12.56e-4;
L=0.44;
G=2;
T=300;
q=1.6e-19;
k=8.62e-5;
Edot=0.05;
Ndot=5e9;
DE=0.25;
syms E;
denom=1+exp((E-(v/L))/(k*T));
function1=1./denom;
static=(G*Ndot)/(sqrt((pi*DE)/2));
ek8eths=((E+Edot+(v/L))/DE).^2;
function2=exp(-2*ek8eths);
final=function1.*function2.*static;
inline_final = vectorize (inline (char (final)));
subtotal = quad (inline_final, 0, 1e12);

and the m-file derivative is:

cdot1(1)=integral(-12);
cdot2(281)=integral(2.05);
for i=2:281
v(i)=-12+(i-1)*0.05;
cdot1(i)=integral(v(i));
cdot2(i-1)=cdot1(i);
cdot(i)=(cdot2(i)-cdot2(i-1))/0.05;
end
When i run the m-file derivative,then the program gives me only one solution.That is wrong because i want in the end a matrix with all the values of cdot.281 values i think must have the matrix cdot.Any ideas?
Thank you!!!

Subject: problem with my code

From: Joerg Buchholz

### Joerg Buchholz

Date: 26 Jan, 2009 14:17:02

Message: 4 of 11

"Theo Kalfas" <k_thodoros@yahoo.gr> wrote in message
:
> cdot1(1)=integral(-12);
> cdot2(281)=integral(2.05);
> for i=2:281
> v(i)=-12+(i-1)*0.05;
> cdot1(i)=integral(v(i));
> cdot2(i-1)=cdot1(i);
> cdot(i)=(cdot2(i)-cdot2(i-1))/0.05;
> end
> When i run the m-file derivative,then the program gives me only one solution.That is wrong because i want in the end a matrix with all the values of cdot.281 values i think must have the matrix cdot.Any ideas?
> Thank you!!!

Just remove the semicolons and see what happens ...

Subject: problem with my code

From: Joerg Buchholz

### Joerg Buchholz

Date: 26 Jan, 2009 14:17:02

Message: 5 of 11

"Theo Kalfas" <k_thodoros@yahoo.gr> wrote in message
:
> cdot1(1)=integral(-12);
> cdot2(281)=integral(2.05);
> for i=2:281
> v(i)=-12+(i-1)*0.05;
> cdot1(i)=integral(v(i));
> cdot2(i-1)=cdot1(i);
> cdot(i)=(cdot2(i)-cdot2(i-1))/0.05;
> end
> When i run the m-file derivative,then the program gives me only one solution.That is wrong because i want in the end a matrix with all the values of cdot.281 values i think must have the matrix cdot.Any ideas?
> Thank you!!!

Just remove the semicolons and see what happens ...

Subject: problem with my code

From: Theo Kalfas

### Theo

(Theo Kalfas)

Date: 26 Jan, 2009 15:26:02

Message: 6 of 11

"Joerg Buchholz" <buchholz@hs-bremen.de> wrote in message <glkgku$p7v$1@fred.mathworks.com>...
> "Theo Kalfas" <k_thodoros@yahoo.gr> wrote in message
> > cdot1(1)=integral(-12);
> > cdot2(281)=integral(2.05);
> > for i=2:281
> > v(i)=-12+(i-1)*0.05;
> > cdot1(i)=integral(v(i));
> > cdot2(i-1)=cdot1(i);
> > cdot(i)=(cdot2(i)-cdot2(i-1))/0.05;
> > end
> > When i run the m-file derivative,then the program gives me only one solution.That is wrong because i want in the end a matrix with all the values of cdot.281 values i think must have the matrix cdot.Any ideas?
> > Thank you!!!
>
> Just remove the semicolons and see what happens ...

Sorry Joerg,the semicolons from where you suggest me to remove?I don't understand completely..Sorry!

Subject: problem with my code

From: Joerg Buchholz

### Joerg Buchholz

Date: 26 Jan, 2009 18:45:03

Message: 7 of 11

"Theo Kalfas" <k_thodoros@yahoo.gr> wrote in message <glkkma$pd$1@fred.mathworks.com>...
> "Joerg Buchholz" <buchholz@hs-bremen.de> wrote in message <glkgku$p7v$1@fred.mathworks.com>...
> > "Theo Kalfas" <k_thodoros@yahoo.gr> wrote in message
> > > cdot1(1)=integral(-12);
> > > cdot2(281)=integral(2.05);
> > > for i=2:281
> > > v(i)=-12+(i-1)*0.05;
> > > cdot1(i)=integral(v(i));
> > > cdot2(i-1)=cdot1(i);
> > > cdot(i)=(cdot2(i)-cdot2(i-1))/0.05;
> > > end
> > > When i run the m-file derivative,then the program gives me only one solution.That is wrong because i want in the end a matrix with all the values of cdot.281 values i think must have the matrix cdot.Any ideas?
> > > Thank you!!!
> >
> > Just remove the semicolons and see what happens ...
>
> Sorry Joerg,the semicolons from where you suggest me to remove?I don't understand completely..Sorry!

By removing all (or specific) semicolons you can see intermediate results that will help you find your bug(s). As an alternative you can use the debugger to step through every line of code.

Subject: problem with my code

From: Theo Kalfas

### Theo

(Theo Kalfas)

Date: 27 Jan, 2009 11:19:01

Message: 8 of 11

the M-file integral is:

function subtotal=integral(v)
A=12.56e-4;
L=0.44;
G=2;
T=300;
q=1.6e-19;
k=8.62e-5;
Edot=0.05;
Ndot=5e9;
DE=0.25;
syms E;
denom=1+exp((E-(v/L))/(k*T));
function1=1./denom;
static=(G*Ndot)/(sqrt((pi*DE)/2));
ek8eths=((E+Edot+(v/L))/DE).^2;
function2=exp(-2*ek8eths);
final=function1.*function2.*static;
inline_final = vectorize (inline (char (final)));
subtotal = quad (inline_final, 0, 1e12);

and the m-file derivative is:

cdot1(1)=integral(-12);
cdot2(281)=integral(2.05);
for i=2:281
v(i)=-12+(i-1)*0.05;
cdot1(i)=integral(v(i));
cdot2(i-1)=cdot1(i);
cdot(i)=(cdot2(i)-cdot2(i-1))/0.05;
end
> > > Just remove the semicolons and see what happens ...
> >
> > Sorry Joerg,the semicolons from where you suggest me to remove?I don't understand completely..Sorry!
>
> By removing all (or specific) semicolons you can see intermediate results that will help you find your bug(s). As an alternative you can use the debugger to step through every line of code.

Thank you again Joerg!!I found the problem...
Now 2 more questions...:-)))))
1)When i run the code then i have the values i want.When i change the value Ndot then i don't have the correct values..The problem is that the new values are not overwritting up to old values..About this i must delete everytime the matrix of the workspace and then i run again the code!!How can i do that?To overwrite the new values automatically?
2)Can i have 2 functions in the same m-file?I want to find the derivative for Ndot=8e9 and Ndot=5e9 and after i must find the sum of these 2 matrixes..

Thank you!!

Subject: problem with my code

From: Joerg Buchholz

### Joerg Buchholz

Date: 27 Jan, 2009 14:02:01

Message: 9 of 11

"Theo Kalfas" <k_thodoros@yahoo.gr> wrote in message
:
> > By removing all (or specific) semicolons you can see intermediate results that will help you find your bug(s). As an alternative you can use the debugger to step through every line of code.
>
> Thank you again Joerg!!I found the problem...
> Now 2 more questions...:-)))))
> 1)When i run the code then i have the values i want.When i change the value Ndot then i don't have the correct values..The problem is that the new values are not overwritting up to old values..About this i must delete everytime the matrix of the workspace and then i run again the code!!How can i do that?To overwrite the new values automatically?
> 2)Can i have 2 functions in the same m-file?I want to find the derivative for Ndot=8e9 and Ndot=5e9 and after i must find the sum of these 2 matrixes..

I do not fully unterstand your questions, but

1) You can clear all variables with the command 'clear all'. You can replace variables in a symbolic expression with the 'subs' command ('help subs').

2) Read the paragraph about subfunctions in 'help function'.

Subject: problem with my code

From: Theo Kalfas

### Theo

(Theo Kalfas)

Date: 1 Feb, 2009 16:57:01

Message: 10 of 11

In message 8 of 9 i have the code where i have often problems.Now my new problem is
when i run the M-file integral without "function subtotal=integral(v)" for v=-1,-2 for example then the program gives me a solution.When i try to run it for a matrix of v 281x1(-12:0.05:2) then i have these errors

??? Error using ==> inlineeval

Error in inline expression ==> array([[67930201350122255859375./549755813888./(772894735361391./1125899906842624+772894735361391./1125899906842624.*exp(50000./1293.*E+200000./431)).*exp(-2.*(10./3.*E-119./3).^2)]....................[67930201350122255859375./549755813888./(772894735361391./1125899906842624+77
2894735361391./1125899906842624.*e

Error in ==> C:\MATLAB6p5\toolbox\matlab\funfun\@inline\feval.m
On line 34 ==> INLINE_OUT_ = inlineeval(INLINE_INPUTS_, INLINE_OBJ_.inputExpr, INLINE_OBJ_.expr);

Error in ==> C:\MATLAB6p5\toolbox\matlab\funfun\quad.m
On line 59 ==> y = feval(f, x, varargin{:});

Error in ==> C:\MATLAB6p5\work\integral.m
On line 28 ==> subtotal = quad (inline_final, 0, 1e13);

Can anyone knows where is the problem in inline_final and subtotal function?
Thank you

Subject: problem with my code

From: Theo Kalfas

### Theo

(Theo Kalfas)

Date: 2 Feb, 2009 09:00:04

Message: 11 of 11

Any ideas for my problem???
Thank you!!!

## Tags for this Thread

### What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.