Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Can anybody help guide me into the right direction for this problem without usin

ID: 669977 • Letter: C

Question

Can anybody help guide me into the right direction for this problem without using the function command?

Piecewise functions are sometimes useful when the relationship between a dependent and an independent variable cannot be adequately represented by a single equation. For example, the velocity of a rocket might be described by where v is the rocket velocity in m/s and t is the time in seconds. Write a program to compute v as a function of t. Generate a plot of v versus t for t = -5 s to 70 s using 100 points for t. Requirement: You Cannot use Logic Operators. You must create v one element at a time inside a for loop. Note that your if structure will be inside your for loop. The only output is your plot. Your plot command should be after your for loop not inside your for loop.

Explanation / Answer

Use the code to solve the problem :

or Try this code once :

cvx_quiet(’true’)

figure

plot(x,y,’k:’,’linewidth’,2)

hold on

% Single line

p = [x ones(100,1)]y;

alpha = p(1)

beta = p(2)

plot(x,alpha*x+beta,’b’,’linewidth’,2)

mse = norm(alpha*x+beta-y)^2

for K = 2:4

% Generate Lagrange basis

a = (0:(1/K):1)’;

F = max((a(2)-x)/(a(2)-a(1)),0);

for k = 2:K

a_1 = a(k-1);

a_2 = a(k);

a_3 = a(k+1);

f = max(0,min((x-a_1)/(a_2-a_1),(a_3-x)/(a_3-a_2)));

F = [F f];

end

f = max(0,(x-a(K))/(a(K+1)-a(K)));

F = [F f];

% Solve problem

cvx_begin

variable z(K+1)

minimize(norm(F*z-y))

subject to

(z(3:end)-z(2:end-1))./(a(3:end)-a(2:end-1)) >=...

(z(2:end-1)-z(1:end-2))./(a(2:end-1)-a(1:end-2))

cvx_end

% Calculate alpha and beta

alpha = (z(2:end)-z(1:end-1))./(a(2:end)-a(1:end-1))

beta = z(2:end)-alpha(1:end).*a(2:end)

% Plot solution

y2 = F*z;

mse = norm(y2-y)^2

if K==2

plot(x,y2,’r’,’linewidth’,2)

elseif K==3

plot(x,y2,’g’,’linewidth’,2)

else

plot(x,y2,’m’,’linewidth’,2)

end

end

xlabel(’x’)

ylabel(’y’)

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote