Question

In: Mechanical Engineering

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

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

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

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

b. x0= 1.0 , step size h=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 exact solution.

I keep getting an error of "Matrix Dimensions must agree ; error in Project_2(my function) with my Tay = ... equation (2nd order taylor equation).

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

Solutions

Expert Solution

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];
[outputArg,Tay] = project_2(y1,h,d,B,t_span);

subplot(3,2,1)
plot(x,y)
xlabel('value of x')
ylabel('value of y(x)')
grid on;

subplot(3,2,3)
plot(outputArg)
xlabel('no of points')
ylabel('outputArg')
grid on;

subplot(3,2,5)
plot(Tay)
xlabel('no of points')
ylabel('Tay')
grid on;


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

subplot(3,2,4)
plot(outputArg)
xlabel('no of points')
ylabel('outputArg')
grid on;

subplot(3,2,6)
plot(Tay)
xlabel('no of points')
ylabel('Tay')
grid on;

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

outputArg = y1 + h.*d; %Euler method

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

end


Related Solutions

I am learning Matlab. I have a math formula such as A(x , y)= (x+1) /...
I am learning Matlab. I have a math formula such as A(x , y)= (x+1) / (y+1), and n=6 How can I write down the for loop in Matlab to express 1 <= x and y < = n? Thanks
Hi, I am running C# in Vis. Studio 2019 community. Trying to get my program to...
Hi, I am running C# in Vis. Studio 2019 community. Trying to get my program to populate the username in the program after entered. I can enter a name and the three scores and average them as the program needs but the name is not adding next to the "Students name: " in the program. Any help would be appreciated and please place a note for what I am doing wrong. Thank you using System; using System.Collections.Generic; using System.Linq; using...
I am trying to get this code to work but I am having difficulties, would like...
I am trying to get this code to work but I am having difficulties, would like to see if some one can solve it. I tried to start it but im not sure what im doing wrong. please explain if possible package edu.hfcc; /* * Create Java application that will create Fruit class and Bread class * * Fruit class will have 3 data fields name and quantity which you can change. * The third data field price should always...
y'=y-x^2 ; y(1)= -4 Write a MATLAB program that makes two plots of the solution to...
y'=y-x^2 ; y(1)= -4 Write a MATLAB program that makes two plots of the solution to the equation using the following values. Suggest you use nested loops instead of two different loops. Be sure to label your plots. a. x0 = 1.0, step size h = .2, number of steps n = 20. b. x0 = 1.0, step size h = .05, number of steps n = 80.
1. I am trying to determine the level of measurement of my data type? I am...
1. I am trying to determine the level of measurement of my data type? I am looking for advice on Nominal, Ordinal, Interval, and Ratio 2. Does the data set have any categorical variables? I am trying to Describe the data set below in very general terms? This data consist of 8 variables: Which are GRE Scores, TOEFL Scores, University Rating, Statement of Purpose, Letter of Recommendation Strength, Undergraduate GPA, . Research Experience, and Chance of Admit. Name Type Description...
I am trying to work a statistics problem for decision modeling class. The main question is:...
I am trying to work a statistics problem for decision modeling class. The main question is: Consider the experiment of drawing two cards without replacement from a deck consisting of only the ace through 10 of a single suit (for example, only hearts). Q - Define the event Ai to be the set of outcomes for which the sum of the values of the cards is i (with an ace = 1). List the outcomes associated with Ai for i=...
This is Using MATLAB: I am trying to store the solution of this matrix because I...
This is Using MATLAB: I am trying to store the solution of this matrix because I want to do something with the result like find the norm of that answer however I am stuck and cannot seem to be able to. Help would be appreciated! --------------------------------------------- MATLAB CODE: close all clear clc A = [1 -1 2 -1; 2 -2 2 -3; 1 1 1 0; 1 -1 4 5]; b = [-8 -20 -2 4]'; x = gauss_elim(A,b) function...
In trying to apply my knowledge in the real world, I am trying to create a...
In trying to apply my knowledge in the real world, I am trying to create a realistic retirement schedule. However, I am running into difficulties using both a financial calculator as well as our equations from class in doing this. I am trying to do the following: plan a retirement schedule between the ages of 25 and 70, in which I would deposit 20% of my income each year. The income starts at 80,000 with an annual growth rate of...
I am trying to start saving for retirement. I am investing all my cash into the...
I am trying to start saving for retirement. I am investing all my cash into the S&P 500, which will assume consistently 9.8% interest, compounded annually. I initially put a lump sum of $100 into my account, and I will deposit $10 every second week. a) After 10 years, how much money will I have invested? b) After 10 years, if I sold all of my stocks, how much money will I have in my account? c) After 25 years,...
Hello Everyone, Can anyone tell me why my program will not run? I am trying to...
Hello Everyone, Can anyone tell me why my program will not run? I am trying to work on abstract base classes... not sure what is going on. import math from abc import ABC from abc import abstractmethod #TODO: convert this to an ABC class Shape(ABC): def __init__(self): self.name = "" def display(self): print("{} - {:.2f}".format(self.name, self.get_area())) #TODO: Add an abstractmethod here called get_area @abstractmethod def get_area(self): if self.name == "Circle": get_area()= 3.14 * radius * radius else: get_area() = self.length...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT