# What am I missing here?

1 ビュー (過去 30 日間)
Zifeng Qiu 2020 年 7 月 4 日

f=@(t,u) t*(u^.2-u+0.5);
[tv,uv]=BE(f,1,10,100);
function [tv, uv] = BE(f, u0, T, n)
dt = T/n; % Differential of time.
tv = transpose(0:dt:T); % Evaluation times.
uv = zeros(n+1, 1); % Solution.
uv(1) = u0; % Initial value
for i=1:length(tv)-1
uv(i+1) = uv(i) + f(tv(i+1), uv(i+1))*dt;
end
end
This is a problem that I was trying to solve on Matlab grader, what am I missing or what did I do wrong in here? This is Backward Euler.

#### 2 件のコメント

John D'Errico 2020 年 7 月 4 日
Note that when you paste in a picture of code, thus a screenshot as you have done, you make it considerably more difficult for somone to help you or to diagnose your problem. That way, they need to actually type in your code all over, taking considerably more time, then if they just needed to copy text from the page. The alterntive provided is to paste in the text of your code, using the tools in Answers to format it as code. In fact, this takes less effort for you to do, than to take screenshots, and then attach them.
Now, given that we assume you actually want someone to help you, logically, why would you make it more difficult to actually get the help you very much want? To me, this seems a truly strange action.
Zifeng Qiu 2020 年 7 月 4 日
Sorry about that, I have changed the code

### 回答 (1 件)

John D'Errico 2020 年 7 月 4 日
Thank you. That makes it much easier for someone to offer you help. Now I'll be willing to look at the problem you had.
It looks like you took the code you wrote for an Euler solve, and then just tried changing this line:
uv(i+1) = uv(i) + f(tv(i+1), uv(i+1))*dt;
As you can (should) see, this is a problem, because it presumes a value on the right hand side, thus uv(i+1), that at that point in the loop, has never been assigned. So, what does MATLAB use there? It uses the zero value you put in that place before the loop ever started, when you preallocated uv as zeros. That is, for every iteration, you caused this to happen:
uv(i+1) = uv(i) + f(tv(i+1), 0)*dt;
And that clearly is NOT what you wanted.
I think you missed the part of the question, where they even prompted you with the trick you need to use. It said to use fzero. (Reread the last line of the question as you give it.) At that point, you don't know the value of uv(i+1). But you do have an equation for it. The value uv(i+1) is the value that satisfies the equation:
uv(i) + f(tv(i+1), uv(i+1))*dt - uv(i+1) = 0
So you need to use fzero to find that value. How would your code change? Almost everything stays the same.
f=@(t,u) t*(u^.2-u+0.5);
[tv,uv]=BE(f,1,10,100);
function [tv, uv] = BE(f, u0, T, n)
dt = T/n; % Differential of time.
tv = transpose(0:dt:T); % Evaluation times.
uv = zeros(n+1, 1); % Solution.
uv(1) = u0; % Initial value
for i=1:length(tv)-1
uv(i+1) = fzero(@(uvsol) uv(i) + f(tv(i+1), uvsol)*dt - uvsol, uv(i));
end
end
Do you see what the for loop now does? Inside the loop, it uses fzero each time, solving for the unknown value of uv(i+1), such that the equation is satisfied.
To choose the starting value for fzero it at each iteration, I used uv(i), based on the assumption that uv(i+1) should be close to uv(i).

Translated by