need mfile for my non linear system
Afficher commentaires plus anciens
I have to employ the newton raphson algorithm to solve my equations,. I need to create a function m-file to do so. This is my first matlab assignment and I'm not really familiar with it. I don't really know how to go about the iteration and I try to write it but matlab not solve. this is my try. Any help would be greatly appreciated.
4 commentaires
Walter Roberson
le 22 Jan 2021
It is disappointing that the user deleted nearly all of their comments from the extended discussion, making it difficult to follow the conversation. The volunteers give their time with an understanding that the discussion will remain public and so be of value to anyone else who looks. When important parts of the discussion are removed then the volunteers tend to start to feel as if they have been taken advantage of.
Walter Roberson
le 22 Jan 2021
hasan s: I notice that you unaccepted Cris's answer, indicating that another answer was better. Could you post a link to the place where you received the better response?
hasan s
le 22 Jan 2021
Walter Roberson
le 22 Jan 2021
However, our solution is for the question as posted, and is useful for people who might want to study the techniques.
It is not uncommon for people to discover that they made a mistake in asking the question when they start thinking about the responses they got. It is also not uncommon that when users see how the volunteers interpret the question that was actually asked, that the user's thought processes are pulled out of the grove they were in, and are able to see how they should have proceeded. Thus, even questions containing mistakes are valuable to readers. Indeed, most questions that are posted contain a mistake of some sort, and figuring out what the mistake is is a lot of what we do here.
Réponses (1)
Cris LaPierre
le 24 Déc 2020
0 votes
You can find examples and explanations about how to create a funciton file here:
13 commentaires
Cris LaPierre
le 24 Déc 2020
I'm not going to write it for you, but if you ask specific questions, I'm happy to answer them.
Before you use a variable on the right side of an equals sign, you have to define it.
% Define a variable x
x=5;
% This works because x has been defined
y=2*x
% This does not because z has not been defined
y=2*z
If you are new to programming in MATLAB, I suggest going through MATLAB Onramp. This will introduce you to the fundamentals in an interative way and at your own pace.
Cris LaPierre
le 24 Déc 2020
That's a warning, not an error. You can ignore that for now.
Onramp will take you ~2 hours. Given the date and the urgency of your assignment, that is going to be 2 hours well spent.
Cris LaPierre
le 24 Déc 2020
- Complete MATLAB Onramp
- Ignore the message about B. This is not an error.
hasan s
le 24 Déc 2020
Jan
le 24 Déc 2020
"Onramp" means the online Matlab lesson to learn the basics: https://www.mathworks.com/learn/tutorials/matlab-onramp.html
Cris LaPierre
le 24 Déc 2020
Remove the semicolon at the end of each statement you would like to see the command window.
In order for results to appear, you need to use one of a few different possibilities:
- disp(expression) to have the results of the expression displayed; or
- fprintf(format, expression) to have the results of the expression displayed as text in a more controlled way; or
- have a calculation expression that is not followed by semi-colon
F = randi([-40 99]);
disp(F) %example of disp
fprintf('Temperature in Fahrenheit is %g\n', F); %example of fprintf
C = (F-32)*5/9 %example of no semi-colon
hasan s
le 24 Déc 2020
Walter Roberson
le 25 Déc 2020
Your file name has a space in it. Untitled 33.m . You need to remove the space.
x=[B(i)+G(i)*(gamma(1+1/A(i)))-sum1;(G(i)^2)*(gamma(1+2/A(i)))
+(2*B(i)*G(i)*gamma(1+1/A(i))
+B(i)^2-sum2);(G(i)^3*gamma(1+3/A(i))+3*B(i)*G(i)^2*gamma(1+2/A(i))+3*G(i)*B(i)^2*gamma(1+1/A(i))+B(i)^3-sum3)];
MATLAB does not continue lines automatically (unlike C, for example).
x=[B(i)+G(i)*(gamma(1+1/A(i)))-sum1;(G(i)^2)*(gamma(1+2/A(i)))
1 0 1 0 1 2 3 320
so the part up to the ; is a valid complete element, and the ; explicitly marks the end of a row
x=[B(i)+G(i)*(gamma(1+1/A(i)))-sum1;(G(i)^2)*(gamma(1+2/A(i)))
1 2 1 0 1 2 3 210
so the (G to end of line is a valid complete element. Inside [], when a line ends with no continuation symbol, that is treated as an implicit end of row, as if you had written a ; at the end of the line. Thus, the above line of code gives two complete matrix rows.
+(2*B(i)*G(i)*gamma(1+1/A(i))
1 2 1 2 1 2 3 21 <== missing )
You are inside [] so MATLAB will try to treat that as a matrix row. However, it is missing a ) to be a valid matrix row. The + at the beginning of the line is not a problem: it is treated as a "unary plus". Just the same way that you can write -5 to indicate 0 minus 5, or -x to indicate 0 minus x, you can write +5 to indicate 0 plus 5, or +x to indicate 0 plus x. In this context, the + does not signal that you are in the middle of an addition.
+B(i)^2-sum2);(G(i)^3*gamma(1+3/A(i))+3*B(i)*G(i)^2*gamma(1+2/A(i))+3*G(i)*B(i)^2*gamma(1+1/A(i))+B(i)^3-sum3)];
1 0 !
Again, the + at the beginning of the line is valid unary operation and does not signal that you are in the middle of an addition. But then you have one extra ) on the matrix element.
What you probably wanted was
x=[B(i)+G(i)*(gamma(1+1/A(i)))-sum1;(G(i)^2)*(gamma(1+2/A(i)))...
+(2*B(i)*G(i)*gamma(1+1/A(i))...
+B(i)^2-sum2);(G(i)^3*gamma(1+3/A(i))+3*B(i)*G(i)^2*gamma(1+2/A(i))+3*G(i)*B(i)^2*gamma(1+1/A(i))+B(i)^3-sum3)];
which in turn would be better written
x=[B(i)+G(i)*(gamma(1+1/A(i)))-sum1;
(G(i)^2)*(gamma(1+2/A(i)))+(2*B(i)*G(i)*gamma(1+1/A(i))+B(i)^2-sum2);
(G(i)^3*gamma(1+3/A(i))+3*B(i)*G(i)^2*gamma(1+2/A(i))+3*G(i)*B(i)^2*gamma(1+1/A(i))+B(i)^3-sum3)];
We are not able to test the code as you did not initialize T or G or B or A.
Also
Program (2)
You should probably be storing the second program in its own file.
Walter Roberson
le 25 Déc 2020
Your code uses i a lot, and stores into
A(i+1)=z(1);
B(i+1)=z(2);
G(i+1)=z(3);
but the only place it changes i is right at the beginning, before the loop, where you have the unusual lines
i=0;
i=i+1;
In your second batch of code, you use Psi but you do not initialize it. Perhaps you wanted psi
In your first program
q=inv(d);
You have a problem because d becomes nearly singular.
Walter Roberson
le 31 Déc 2020
I did not write gamma "instead" of anything. Your code had
x=[B(i)+G(i)*(gamma(1+1/A(i)))-sum1;(G(i)^2)*(gamma(1+2/A(i)))
+(2*B(i)*G(i)*gamma(1+1/A(i))
+B(i)^2-sum2);(G(i)^3*gamma(1+3/A(i))+3*B(i)*G(i)^2*gamma(1+2/A(i))+3*G(i)*B(i)^2*gamma(1+1/A(i))+B(i)^3-sum3)];
which already has gamma in it. I reformatted,
x=[B(i)+G(i)*(gamma(1+1/A(i)))-sum1;
(G(i)^2)*(gamma(1+2/A(i)))+(2*B(i)*G(i)*gamma(1+1/A(i))+B(i)^2-sum2);
(G(i)^3*gamma(1+3/A(i))+3*B(i)*G(i)^2*gamma(1+2/A(i))+3*G(i)*B(i)^2*gamma(1+1/A(i))+B(i)^3-sum3)];
which is just moving around the line breaks.
q=inv(d);
p=q*x;
cannot be replaced by x/d but can be replaced by
p=d\x;
No,
A(i+1)=z;
B(i+1)=z;
G(i+1)=z;
is not a replacement for the existing lines. z is a vector of length 3, but A(i+1) only designates a single output location. You also want A, B, and G to get different outputs, but assigning z to all of them would give the same value to each.
What you could do is
zcell = num2cell(z(1:3));
[A(i+1), B(i+1), G(i+1)] = zcell{:};
but this is less clear than just doing three assignment statements.
T=1;
A=2;
B=3;
G=4;
That will not work. You have
for j=1:50
sum1=sum1+log((T(j)-B(i))/G(i));
When j becomes 2, you need to access T(2) but T has only been initialized as a scalar. You never store into T after the initial assignment.
hasan s
le 6 Jan 2021
Catégories
En savoir plus sur File Operations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!