MATLAB Solving Matlab Noob Question: Updating Variables with deltax

  • Thread starter Thread starter danbone87
  • Start date Start date
  • Tags Tags
    Matlab Noob
AI Thread Summary
The discussion revolves around a MATLAB code intended for solving a system of nonlinear equations iteratively. The user seeks assistance on how to update the values of variables a, b, and C within a vector d during each iteration of a while loop. The main issue identified is that the solution vector d is not being updated, which could cause the loop to run indefinitely. To resolve this, it is suggested to modify the code by adding a line to update d with the new deltax values in each iteration. Specifically, the recommendation is to set d equal to itself plus deltax, ensuring that a, b, and C are redefined from d at each step. Additionally, it is advised to keep the recorded times and concentrations outside the loop since they remain constant throughout the iterations. This approach aims to ensure that the iterative process converges correctly by continually updating the solution vector.
danbone87
Messages
28
Reaction score
0

Homework Statement



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...
 
Physics news on Phys.org
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.
 

Similar threads

Replies
4
Views
1K
Replies
18
Views
4K
Replies
9
Views
5K
Replies
6
Views
2K
Replies
2
Views
1K
Replies
1
Views
2K
Replies
2
Views
2K
Back
Top