Question

In: Advanced Math

function[prob,flop]=Matlab(a,b) a=[1,2,3]; b=[7,8,9]; if (length(a)~=length(b)) disp("it doesn't make sense"); end prob=0; flop=0; for i=1:length(a) prob=prob+a(i)*b(i); flop=flop+2;...

function[prob,flop]=Matlab(a,b)

a=[1,2,3];
b=[7,8,9];

if (length(a)~=length(b))
disp("it doesn't make sense");
end

prob=0;
flop=0;

for i=1:length(a)
prob=prob+a(i)*b(i);
flop=flop+2;
end
disp(prob);
disp(flop);

What is wrong with my code? it only run the disp(prob) and it doesn't run the disp(flop)

here is what I got

ans =

50

However, if I take % for the function line, it shows

50

6

Solutions

Expert Solution

function dummy
clc;
clear all;
a=[1;2;3];
b=[7;8;9];

[prob,flop]=Matlab(a,b);
function[prob,flop]=Matlab(a,b)

if (length(a)~=length(b))
disp("it doesn't make sense");
end

prob=0;
flop=0;

for i=1:length(a)
prob=prob+a(i)*b(i);
flop=flop+2;
end
disp(prob);
disp(flop);
end
end

%%%%%%%%%%%%%% Answer

50

6

>>

you should not give a=[1,2,3]; b=[7,8,9]; .. you should use by this way a=[1;2;3]; b=[7;8;9];

function dummy
clc;
clear all;
a=[1,2,3];
b=[7,8,9];

[prob,flop]=Matlab(a,b);
function [prob,flop]=Matlab(a,b)

if (length(a)~=length(b))
disp("it doesn't make sense");
end

prob=0;
flop=0;

for i=1:length(a)
prob=prob+a(i)*b(i);
flop=flop+2;
end
disp(prob);
disp(flop);
end
end

$$$$$$$$ Note: this way also its give same answer


Related Solutions

Pycharm Write a function to make a new list 1. if original_samples is [1,2,3], then this...
Pycharm Write a function to make a new list 1. if original_samples is [1,2,3], then this should return a new list [3,2,1]. Don't use the reversed function or original_samples[::-1] def make_reserved_samples (original_samples): new_samples[ ]    return new_samples 2. multiplied by volume. If original_samples is [1,2,3] and volume is 2, the new list is [2,4,6] def make_louder_samples(original_samples, volume): return [ ] 3. If the original list is [1,2,3,4,5,6,7,8] and skip is 3, the new liat should be [1,4,7]. Don't use original_list[::skip]....
1. Discuss:"At first glance, drug tolerance doesn't seem to make sense. If a person takes a...
1. Discuss:"At first glance, drug tolerance doesn't seem to make sense. If a person takes a drug to feel good but that drug is not effective at increasing dopamine, then what is the point of taking the drug?" 2. How do environmental cues elicit a craving for drugs? How does this relate to relapse?
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). B) use...
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). B) use Gauss-siedel iteration. C)use SOR with w=1.25, w=1.5, w=1.75,w=1.9, and optimal value if given. * A=[4,8,0,0;8,18,2,0;0,2,5,1.5;0,0,1.5,1.75] , B=[8;18;0.5;-1.75]. , (optimal w is 1.634.)
How can I make my own function for rbinom(n, size, prob) in R. So, basically I...
How can I make my own function for rbinom(n, size, prob) in R. So, basically I want to build my own rbinom function that gives me the same result as rbinom that's built in R software.
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). A)use Jacobi...
Use ten iterations of the appropriate MATLAB function, with x^(0)=[0,...,0]', to solve Ax=b (approximately). A)use Jacobi iteration. B) use Gauss-siedel iteration. 1) make sure to use SOR with w=1.25, w=1.5, w=1.75,w=1.9, and optimal value if given. * A=[1,-2,0,0;-2,5,-1,0;0,-1,2,-0.5;0,0,-0.5,1.25]] , B=[-3;5;2;3.5]. , (optimal w is 1.5431.)
Prob 1: Do not work on problem 1. I would you to work on problem 2...
Prob 1: Do not work on problem 1. I would you to work on problem 2 You have been given the following information on a project: It has a five-year lifetime The initial investment in the project will be $25 million, and the investment will be depreciated straight line, down to a salvage value of $10 million at the end of the fifth year. The revenues are expected to be $20 million next year and to grow 10% a year...
Given the following periodic function; ?(?) = ?, 0 ≤ ? < 1 ?(?−1)=?(?), ?≥1 i....
Given the following periodic function; ?(?) = ?, 0 ≤ ? < 1 ?(?−1)=?(?), ?≥1 i. Sketch the graph of the function, and ii. Find its Laplace Transform
Matrix: Ax b [2 1 0 0 0 | 100] [1 1 -1 0 -1 |...
Matrix: Ax b [2 1 0 0 0 | 100] [1 1 -1 0 -1 | 0] [-1 0 1 0 1 | 50] [0 -1 0 1 1 | 120] [0 1 1 -1 1 | 0] Problem 5 Compute the solution to the original system of equations by transforming y into x, i.e., compute x = inv(U)y. Solution: %code I have not Idea how to do this. Please HELP!
a.)Find the length of the spiral r=θ for 0 ≤ θ ≤ 2 b.)Find the exact...
a.)Find the length of the spiral r=θ for 0 ≤ θ ≤ 2 b.)Find the exact length of the polar curve r=3sin(θ), 0 ≤ θ ≤ π/3 c.)Write each equation in polar coordinates. Express as a function of t. Assume that r>0. - y=(−9) r= - x^2+y^2=8 r= - x^2 + y^2 − 6x=0 r= -    x^2(x^2+y^2)=2y^2 r=
Let f(x, y) be a function such that f(0, 0) = 1, f(0, 1) = 2,...
Let f(x, y) be a function such that f(0, 0) = 1, f(0, 1) = 2, f(1, 0) = 3, f(1, 1) = 5, f(2, 0) = 5, f(2, 1) = 10. Determine the Lagrange interpolation F(x, y) that interpolates the above data. Use Lagrangian bi-variate interpolation to solve this and also show the working steps.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT