y'=y-x^2 ; y(1)= -4 My MATLAB program won't work. I am trying to get the main program to...

90.2K

Verified Solution

Question

Mechanical Engineering

y'=y-x^2 ; y(1)=-4

My MATLAB programwon't work. I am trying to get the main program to output a plot ofthe three equations (1 from the main program and two called in thefunction). The goal is to code a Euler method and a 2nd orderTaylor numerical solution for

a. x0= 1.0 , step sizeh= 0.2, # of steps n=20

b. x0= 1.0 , step sizeh=0.05 , # of steps n=80 ; write a separate functionn for f(x,y)that is called. Plot the results on the same plot as the exactsolution.

I keep getting anerror of \"Matrix Dimensions must agree ; error in Project_2(myfunction) with my Tay = ... equation (2nd order taylorequation).

Main Code

t_span = 1:0.2:5;
h=0.2;
y1 = -4;
B=(t_span.^2);
[x,y] = ode45(@(x,y) y-x^2, t_span, y1);
d=[x,y];
project_2(y1,h,d,B)

subplot(4,1,1)
plot(x,y)

xlabel('value of x')
ylabel('value of y(x)')
grid on

t_span = [1:0.05:5];
y1 = -4;
h=0.05;
[x,y]= ode45(@(x,y) y-x^2, t_span, y1);
subplot(4,1,4)
project_2(y1,h,d,B)
plot(x,y)
xlabel('value of x')
ylabel('value of y(x)')
grid on

The Function

function[outputArg,Tay] = project_2(y1,h,d,B)

outputArg = y1 + h*d; %Euler method

Tay= y1+(h*d)+((1/2)*(h^2))*((y1-2*t_span)+(-B)*d); %2nd order Taylor

subplot(4,1,2)
plot(outputArg)

subplot(4,1,3)
plot(Tay)

end

Answer & Explanation Solved by verified expert
4.3 Ratings (561 Votes)
tspan 1025h02y1 4Btspan2xy ode45xy yx2 tspan y1dxyoutputArgTay project2y1hdBtspansubplot321plotxyxlabelvalue of    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students