フィルターのクリア

How do I get all functions in this code on one figure? It is only displaying the plot from the first function.

1 回表示 (過去 30 日間)
function question1
y=1;
h=.02;
t=0
tic
for i=1:2500
hold on
t(i+1)=t(i)+h;
y(i+1)=y(i)+h*f(t(i),y(i))
plot(t,y,'r--')
xlabel('t'); ylabel('y')
title('Eulers Method for function question1')
end
function actual=g(t,y)
actual=((5*ones*exp(-(2*t)))-(3*ones*exp(-(4*t))))/2;
figure(2)
hold all
plot(t,y);
function question1=f(t,y)
question1=(3*ones*exp(-(4*t)))-2*y;
function error=h(t,error)
error=abs(actual-question1)/actual;
figure(3)
plot(t,error,':')
xlabel('t') % Labels ??x?? axis
ylabel('Error') % Labels ??y?? axis
title('Error using Euler Method');
  2 件のコメント
Mohannad Abboushi
Mohannad Abboushi 2016 年 2 月 5 日
The bottom code is supposed to be included as well. Sorry I am a beginner in Matlab.
Muhammad Fahad
Muhammad Fahad 2016 年 3 月 26 日
編集済み: Walter Roberson 2016 年 3 月 26 日
Actually I want to solve the system of two coupled differential equations in MATLAB by using implicit Euler's Method.My teacher suggests me to use the command "fsolve".Here I am providing the MATLAB code that I construct.I know there must be a very stupid error at line 13 but anyways help me to solve this problem: clear all clc
f = @(y) [-80.6*y(1)+119.4*y(2); 79.6*y(1)-120.4*y(2)];
h=1/100;
y(:,1)=[1 ; 4]; t(1)=0;
for i =1:2
y(:,i+1)=fsolve(@(z) -z+y(:,i)+h*f(z),[-10 10])
t(i+1)=t(i)+h;
end
plot(t,y(1,:),'b',t,y(2,:),'b')
hold on
ts=0:0.001:1;
ys(1,:)=(3)*exp(-ts)+(-2)*exp(-200*ts);
ys(2,:)=(2)*exp(-ts)+(2)*exp(-200*ts);
plot(ts,ys(1,:),'r',ts,ys(2,:),'g')

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2016 年 2 月 6 日
When you used
h=.02;
you "overshadowed" the meaning of h as a function, so your h function will never be called.
You have no call to your "g" function.
Caution: you start your "g" function with "hold all" after the figure(2). When you have a hold all before you have plotted anything, the default axes limits of [0 1] are frozen and nothing outside of that limit will be plotted. You should not use "hold all" or "hold on" unless you are certain you have plotted something up to the maximum range already, or unless you have set the axes limits manually using xlim() / ylim() or set() of the axes XLim and YLim properties.
  2 件のコメント
Image Analyst
Image Analyst 2016 年 2 月 6 日
"hold all" has been deprecated and the help recommends using "hold on" from now on.
Don't use "error" as the name of a variable since it is a crucial built-in function.
Walter Roberson
Walter Roberson 2016 年 3 月 26 日
Note: The "hold on" change is from R2014b.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by