Solving Matlab Noob Question: Updating Variables with deltax

  • Context: MATLAB 
  • Thread starter Thread starter danbone87
  • Start date Start date
  • Tags Tags
    Matlab Noob
Click For Summary
SUMMARY

This discussion focuses on updating variables in a MATLAB script for solving a system of nonlinear equations. The user initially struggles with ensuring that the vector 'd' updates correctly within a loop. The solution involves modifying the script to include 'd = d + deltax' to ensure that the values of 'a', 'b', and 'C' are updated in each iteration. Additionally, it is recommended to move the definitions of 'Y' and 't' outside the loop for efficiency.

PREREQUISITES
  • Familiarity with MATLAB syntax and operations
  • Understanding of nonlinear equations and residuals
  • Knowledge of Jacobian matrices and their applications
  • Basic concepts of iterative methods in numerical analysis
NEXT STEPS
  • Learn about MATLAB's matrix operations and how to manipulate arrays
  • Study the implementation of Newton's method for solving nonlinear equations
  • Explore MATLAB's built-in functions for numerical optimization
  • Investigate best practices for structuring MATLAB scripts for performance
USEFUL FOR

This discussion is beneficial for MATLAB users, particularly those working on numerical methods, engineers, and students tackling nonlinear equation systems in their projects.

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 ·
Replies
4
Views
2K
  • · Replies 18 ·
Replies
18
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
2K
Replies
5
Views
8K