MATLAB Matlab noob question.

  • Thread starter danbone87
  • Start date
1. The problem statement, all variables and given/known data

Here's my code

clc
clear
n=10
a=input( 'initial guess for a ');
b=input( ' initial guess for b ');
C= input( ' initial guess for Steady State Concentration ');
d=[a
b
C]
while n>0

t=[3
9
12
18
24
300
]; %times recorded

Y=[4.1
4.3
3.9
3.4
3.1
2.7]; %Concentrations recorded

R=[(-Y+C+a.*exp(-.47.*t)+b.*exp(-0.6e-1.*t)).^2] %Residual vector

J1= [(2.*(-Y+C+a.*exp(-.47.*t)+b.*exp(-0.6e-1.*t))).*exp(-.47.*t)]; %partial with respect to a

J2= [(2.*(-Y+C+a.*exp(-.47.*t)+b.*exp(-0.6e-1.*t))).*exp(-0.6e-1.*t)]; %partial with respect to b

J3= [(-2.*Y)+(2.*C)+(2.*a.*exp(-.47.*t))+(2.*b.*exp(-0.6e-1.*t))]; %partial with respect to C
Jacobian= [J1 J2 J3]

JT= [J1 J2 J3]' %transpose Jacobian

JA=inv(JT*Jacobian) %inverse Jacobian*transpose Jacobian

deltax=[JA*JT*R]

if d-deltax <.0001
a=
if d-deltax >.0001
break

end
end
n=n-1
end

My question is how would I make my a, b and c values inside the "d" vector = to each value of my deltax vector within the loop such that they are replaced for every iteration?

I can't remember anything from matlab. it's been 2 years...
 
212
0
What exactly are you trying to do? It looks like you're trying to iteratively solve a system of nonlinear equations? I think your question is about why your solution vector d is not updating. It sounds like you want to add something like 'd = d + deltax' so that d changes at each step of the loop, otherwise your loop will just run forever because nothing is changing. But since you're using a,b,c instead of d, you also need to redefine a,b,c at each step.

I would change it to something like this (changes in bold):
Code:
clear
n=10
a=input( 'initial guess for a ');
b=input( ' initial guess for b ');
C= input( ' initial guess for Steady State Concentration ');
d=[a b C]
t=[3 9 12 18 24 300 ]; %times recorded
Y=[4.1 4.3 3.9 3.4 3.1 2.7]; %Concentrations recorded

while n>0
[B]a = d(1);
b = d(2);
c = d(3);[/B]
    
R=[(-Y+C+a.*exp(-.47.*t)+b.*exp(-0.6e-1.*t)).^2] %Residual vector

J1= [(2.*(-Y+C+a.*exp(-.47.*t)+b.*exp(-0.6e-1.*t))).*exp(-.47.*t)]; %partial with respect to a

J2= [(2.*(-Y+C+a.*exp(-.47.*t)+b.*exp(-0.6e-1.*t))).*exp(-0.6e-1.*t)]; %partial with respect to b

J3= [(-2.*Y)+(2.*C)+(2.*a.*exp(-.47.*t))+(2.*b.*exp(-0.6e-1.*t))]; %partial with respect to C
Jacobian= [J1 J2 J3]

JT= [J1 J2 J3]' %transpose Jacobian

JA=inv(JT*Jacobian) %inverse Jacobian*transpose Jacobian

deltax=[JA*JT*R]

[B]d = d + deltax;[/B]

if d-deltax <.0001
a=
if d-deltax >.0001
break
end
end
n=n-1
end
I don't know if that's exactly how you want to update d, but you should do something like that. Also, you should move Y = [...] and t = [...] outside of the loop because they are not changing.
 

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top