Can anyone help me with this matlab code?

Hello out there! I have a problem with following code. I have tre equations, which I have to find solutions to with help from newtons method in matlab. It's keep telling me, that I have a undefined variable.
function x = NEWT(x0,k)
x=x0;
for i=1:k
b=F(x);
s=DF(x)\b;
x=x-s;
end
end
function y = F(x)
y=zeros(3,1); % 0-punkter i 3x1 matrix - it is a vector
y(1)=2*x(1)^2-4*x(1)+x(2)^2+3*x(3)^2+6*x(3)+2;
y(2)=x(1)^2+x(2)^2-2*x(2)+2*x(3)^2-5;
y(3)=3*x(1)^2-12*x(1)+x(2)^2+3*x(3)^2+8;
end
function dy = DF(x)
dy=zeros(3,3);
dy(1,1)=4*x(1)-4;
dy(1,2)=2*x(2);
dy(1,3)=6*x(3)+6;
dy(2,1)=2*x(1);
dy(2,2)=2*x(2)-2;
dy(2,3)=4*x(3);
dy(3,1)=6*x(1)-12;
dy(3,2)=2*x(2);
dy(3,3)=6*x(3);
end

8 Kommentare

Image Analyst
Image Analyst am 2 Dez. 2012
How are you calling it? What values for x0 and k are you passing in?
Walter Roberson
Walter Roberson am 2 Dez. 2012
Which variable is being reported as undefined, and at which line?
Angelina
Angelina am 2 Dez. 2012
Bearbeitet: Image Analyst am 2 Dez. 2012
LINE 7, I call my function NEWT. I tried with different values for x0 = 2 and else, but it keeps saying its undefined. And when I have to make my script work I just press on run. I'm very new in matlab.
Image Analyst
Image Analyst am 2 Dez. 2012
You can't do that. When it requires arguments you must first save your function then call it from the command line or another script or function and supply those arguments. You can't simply press the green run triangle button.
Angelina
Angelina am 2 Dez. 2012
Can you tell me how to do that... Step by step :(
Image Analyst
Image Analyst am 2 Dez. 2012
Edit and save your function by clicking on the disk icon. Then click in the command window panel. Type this (without the >> of course because that's just the MATLAB prompt):
>> NEWT(2, 3)
You can use different numbers than 2 and 3 - use whatever you think you should, I just used those as an example. It should report some errors. You fix those errors and repeat the above process until it runs to your satisfaction.
Angelina
Angelina am 2 Dez. 2012
So after fixing the errors, I would get what I want? Thanks a lot for the help!
Image Analyst
Image Analyst am 2 Dez. 2012
Of course. If you don't then there are still errors -- either errors in your logic and algorithm, or errors in the syntax.

Melden Sie sich an, um zu kommentieren.

Antworten (1)

Image Analyst
Image Analyst am 2 Dez. 2012

0 Stimmen

If you say NEWT(2,3) then x = 2, but in F(x) you say y(1)=2*x(1)^2-4*x(1)+x(2)^2+3*x(3)^2+6*x(3)+2 so then x must be an array, not a single number. What are your plans regarding that?

1 Kommentar

Angelina
Angelina am 2 Dez. 2012
This is my exercise: (Computer problem 2.7.4 in [1]): Use Newton's method to find the two
solutions of the system. \begin{equation*} 2u^2 - 4u + v^2 + 3w^2 + 6w + 2 = 0 \end{equation*}
\begin{equation*} u^2 + v^2 - 2v + 2w^2 - 5 = 0 \end{equation*}
\begin{equation*} 3u^2 - 12u + v^2 + 3w^2 + 8 = 0 \end{equation*}
And I didn't thought that I should put any values x0= something, but my lector told me that I had to find a point in R^3. Hmm I don't mean it is an matrix. I have three equations, which is my array, so I wouldn't call my x an array.

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Images finden Sie in Hilfe-Center und File Exchange

Gefragt:

am 2 Dez. 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by