Solving Matlab Noob Question: Updating Variables with deltax

  • MATLAB
  • Thread starter danbone87
  • Start date
  • Tags
    Matlab Noob
In summary, the conversation is discussing how to make the a, b, and C values inside the "d" vector equal to each value of the deltax vector within a loop. The solution is to redefine a, b, and C at each step of the loop and move the Y and t values outside of the loop since they are not changing.
  • #1
danbone87
28
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
  • #2
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.
 
  • #3


I understand that learning new concepts and languages can be challenging, especially after a period of time has passed. It's great that you are trying to refresh your knowledge and continue learning.

To answer your question, you can update the values of your variables within the "d" vector by using the assignment operator "=" in combination with the index of the variable you want to update. For example, if you want to update the value of "a" in the first iteration, you can use "d(1) = a" after calculating your deltax vector. This will replace the first value in "d" with the updated value of "a". You can do the same for "b" and "C" by using "d(2) = b" and "d(3) = C", respectively.

Additionally, I would recommend using descriptive variable names to make your code more readable and easier to understand. This will also help you remember what each variable represents and how it is being used in your code.

I hope this helps and good luck with your coding!
 

1. What is a Matlab Noob Question?

A Matlab Noob Question refers to a question asked by a beginner in Matlab programming. It usually involves basic concepts or troubleshooting issues that are common for new users.

2. How do I update variables with deltax in Matlab?

To update a variable with deltax in Matlab, you can use the assignment operator "=" followed by the variable name and the new value. For example, if you want to update a variable named "x" with a value of 5, you would write "x = 5" in your code.

3. What is the purpose of using deltax in Matlab?

Deltax is commonly used in numerical methods and calculations in Matlab. It represents a small change or increment in a variable's value, which is useful for solving equations or performing iterations.

4. Can I update multiple variables with deltax at once?

Yes, you can update multiple variables with deltax in a single line of code in Matlab. Simply separate the variable names and their new values with commas. For example, "x = 5, y = 3" would update both x and y variables with new values.

5. Is there a specific syntax for updating variables with deltax in Matlab?

Yes, the general syntax for updating variables with deltax in Matlab is "variable = variable + deltax", where "variable" is the name of the variable you want to update and "deltax" is the value you want to add to it.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
1K
  • Calculus and Beyond Homework Help
Replies
4
Views
967
  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
122
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Back
Top